DEV Community

Cover image for Building a TCP Group Chat App with JavaFX and Maven
SikorSky
SikorSky

Posted on

Building a TCP Group Chat App with JavaFX and Maven

Building a Real-Time Group Chat with Java TCP Sockets and JavaFX

We recently implemented a mini project where the objective was to build a real-time group chat application using Java TCP sockets and JavaFX. This article summarizes the architecture, design decisions, and lessons learned during development.


Project Goal

The project required implementing a complete TCP-based chat system with the following capabilities:

  • A central TCP server
  • Multiple concurrent clients
  • Real-time message broadcasting
  • A command to list active users (allUsers)
  • Read-only mode when a client connects without a username
  • JavaFX interfaces for both server and client
  • Proper separation between networking logic and UI

The main goal was to combine network programming with clean application architecture.


Tech Stack

The system was built using the following technologies:

  • Language: Java 17
  • Networking: Java TCP Sockets
  • UI Framework: JavaFX (GridPane + CSS styling)
  • Build Tool: Maven
  • IDE: IntelliJ IDEA

High-Level Architecture

The application follows a Client–Server architecture.

  • The server accepts multiple socket connections.
  • Each client connection is handled in a dedicated thread.
  • The server broadcasts incoming messages to all connected clients.
  • The client UI displays messages and connection status.

This architecture ensures that the system can support multiple users simultaneously while maintaining real-time communication.


MVC / Separation of Concerns

To keep the application maintainable, we structured both the server and client using a Model–View–Controller approach.

Model

Handles:

  • TCP sockets
  • Network input/output
  • Chat protocol commands

View

Contains only:

  • JavaFX UI components
  • Layout and CSS styling

Controller

Responsible for:

  • Connecting UI events to model logic
  • Updating the interface when new messages arrive

Separating networking logic from UI logic made the system much easier to debug and extend.


Key Features Implemented

1. Multi-Client Chat

The server supports multiple simultaneous clients.

All messages are broadcast using the following format:

[HH:mm:ss] username: message
Enter fullscreen mode Exit fullscreen mode

This timestamped format improves readability during conversations.


2. Read-Only Mode

If a user connects without providing a username, the client automatically switches to READ-ONLY MODE.

In this mode:

  • The user can receive messages
  • The user cannot send messages

This allows users to observe the chat without actively participating.


3. allUsers Command

When a client sends the command:

allUsers
Enter fullscreen mode Exit fullscreen mode

The server responds only to that client with the list of active users currently connected to the chat.


4. Graceful Disconnection

Clients can close the session cleanly by typing:

bye
Enter fullscreen mode Exit fullscreen mode

or

end
Enter fullscreen mode Exit fullscreen mode

This ensures that:

  • The socket closes correctly
  • The user is removed from the server's active list
  • The UI updates accordingly

5. User-Friendly Status Indicators

Both server and client applications include UI indicators such as:

  • Online / Offline connection status
  • A live list of connected users
  • Random color highlighting for users on the server dashboard

These features improve usability and monitoring.


Configuration

Server network parameters are externalized using a configuration file:

server.properties
Enter fullscreen mode Exit fullscreen mode

This allows parameters such as host and port to be modified without recompiling the application.


Build and Run

Both server and client are packaged as separate Maven projects.

Server

cd TCPServer
mvn clean package
java -jar target/tcpserver-1.0.0.jar
Enter fullscreen mode Exit fullscreen mode

Client

cd TCPClient
mvn clean package
java -jar target/tcpclient-1.0.0.jar localhost 3000
Enter fullscreen mode Exit fullscreen mode

Challenges We Faced

Concurrency and Connection Lifecycle

Handling multiple client connections while maintaining stable communication required careful management of thread lifecycle and connection states.


Model–View Decoupling

Directly updating the JavaFX UI from networking threads can lead to unstable behavior.

To solve this, we used callback interfaces and controllers to route updates safely to the UI layer.


Packaging JavaFX Applications

When packaging JavaFX with shaded JARs, module overlap warnings may appear.

These warnings are common in JavaFX builds and typically do not affect runtime behavior.


What We Learned

This project provided valuable experience with:

  • Real-world socket programming
  • Managing multiple concurrent clients
  • Designing applications with MVC architecture
  • Keeping network logic independent from UI

One key takeaway is that clean architecture significantly simplifies debugging and future improvements.


Future Improvements

Potential improvements for the project include:

  • Private messaging commands (/msg username message)
  • Persistent chat history
  • Migrating to non-blocking NIO sockets
  • Adding automated integration tests

Final Thoughts

Building a chat system is one of the best ways to learn network programming and concurrency.

This project combined:

  • Networking
  • Desktop UI development
  • Concurrency
  • Software architecture

For developers learning Java networking, implementing a multi-client chat application is an excellent hands-on exercise that builds strong practical skills.

Top comments (0)