Simple multi user chat example using JavaFx and RMI

In this tutorial, we will see the sample code to develop simple multi user chat application using java RMI and javafx.  In this example, we will develop two applications
1. RMIChatServer
2. RMIChatClient
RMIChatServer &  RMIChatClient both applications have  remote interfaces  so that the methods can be invoked  from remote by each other application i.e In  RMIChatServer methods are required to access by RMIChatClient and vice versa.
RMIChatServer has the remote interface called ChatServer which will have the following methods
The above methods will be invoked by RMIChatClient
RMIChatClient has the remote interface called ChatClient which  will have the following methods

The above methods will be invoked by RMIChatServer

Any chat user can join to chat room by accessing RMIChatClient application by giving their mobile number (or any unique number)

Online users list is updated when a chat user is joined or removed to/from the chat room.

Now let us see the code for RMIChatServer and RMIChatClient applications

RMIChatServer Application

ChatServer.java

 

ChatServerImpl.java

 

MyChatServer.java

 

RMIChatClient Application

ChatClient.java

 

ChatClientGui.java

 

ChatClientImpl.java

 

ChatWindow.java

 

Util2.java

 

Running the Application in Eclipse

Change the chatServerUrl  to “rmi://server_ip:3001/RMIChatServer“;   in the ChatClientGui.java file where server_ip is the IP address of the machine where where the  RMIChatServer is running

Make  RMIChatServer  jar and place it on RMIChatClient  application.

Make RMIChatClient jar and place it on RMIChatServer application

Now run the RMIChatServer  application in remote machine. Now RMIChatServer service will be running the on the port 3001.

Now the run the RMIChatClient in any client desktop to join chat room. Give unique number to identify yourself.

Now again the run the RMIChatClient in the any other client machine or same. Give unique number to identify yourself.

You can run any number of RMIChatClient  application from different or same client machine to join chat room.

Now you will see the list of users online. Double click on any number send message to the chat user.

Closing the chat room will disconnect from the server.

Output:

Chat users 11 and 22 are connected with RMI chat server and chatting with each other.

Note: The above code will give an idea of RMI chat. It may require lot of improvement

 

Reference:  Java RMI

JavaFx

You may also like

Leave a Reply