Go to Github

A chat system, what for?

A chat system (instant messaging) allows two or more people to contact each other through a network.
It is possible to communicate on different types of networks:

  • Local or private network
  • Internet or public network

We will focus on the second possibility of communication, which offers an enormous range of possibilities.

Centralized communication with the client-server

A chat application called "client-server" is composed of two applications:

  • A client program. Installed on a user's machine, it allows him to connect to the chat server and communicate with other connected users
  • A server program. Installed on a server, it supports 0 to N incoming connections (with N the maximum number of clients). In addition, it receives messages from one user and transmits them to all other connected users: it is said that the server is broadcasting a message

EasyChat, client-server chat application

EasyChat is a client-server application that allows users to communicate with each other over the Internet.
EasyChat is therefore composed of a client program ("ClientApplication") and a server ("ServerApplication").

An Model View Controller View architecture

The two applications that make up EasyChat are built using a "MVC" pattern for "Model View Controller".

By following this pattern:

  • the model stores data related to a user
  • the view displays interface(s) with information such as messages and user names
  • the controller processes the connection logic

On the server side

The first part of the application is a program that runs on a server. After starting, the server-side program listens to incoming connections in a loop: a message "Waiting for a connection..." is displayed.

On the customer side

The second part of the application is the client program that the user runs from his machine (desktop, laptop, smartphone). The interface of the application looks like this:

When the client application opens, the login page is displayed.
To connect, you must enter a nickname, the address of the chat server (server that runs the server application) and click on the login button.

Connecting a client to the server

First, let's introduce the interface:

  • the top part of the chat window displays the different messages sent and received.
  • The lower part of the window has a "Leave" button to connect and return to the login page, a text field to write a message and a "Send" button to send a written message in the text field.
When a client connects, it may notice that the server has sent a welcome message "Welcome to Easy Chat! You are the number one customer. Here's what happened:
  • the server has received the user's connection request
  • the server has opened a "communication channel", called "socket"
  • the server sends a welcome message

Sending a message to the server

Now that the user has logged in under his nickname (here, John), he decides to send the message "Hello, I'm John" to the server.

The server (console window with black background) has received John's message. It is about to broadcast John's message to all connected users. Unfortunately John is currently the only one connected to the chat server.

Connecting a second client

So we will make sure that a second client connects to the chat server so that it can have a conversation with John. For the sake of this example, user Doe decides to connect to the chat.

Communication between two clients with the server

Doe successfully connects and realizes that he is the number two customer. He decides to send a greeting to John "Hello John, how are you?"
At this point, the server receives the message and resends it to the connected users, John and Doe. Thus, John successfully received Doe's message and was able to respond, "Fine. Let's go to San Francisco".

Notes

In this article, the example used shows that the client connects to the server at address 127.0.0.0.1, i.e. "localhost" or the machine on which we are working locally (e.g. our personal computer).
The client application can work very well with any IPv4 address: its operation is not limited to a single machine but to an entire network.
In particular, I recommend that you test the application on a local network with at least:

  • a server that has an address accessible from the local network
  • two clients that connect to the chat server address

Possible improvements

Of course, there are many improvements that can be made to this application:

  • a history system could be created to save received messages, client side AND server side, or only server side if the latter has the necessary processing capacity
  • a chat room system would allow users to send each other messages that are exclusively visible to members present at a chat room

Easy Chat