I am trying to develop an Instant messenger using WebSocket.
I have multiple instances of my servers running (say server1 , server2). Two users(say userA , userB) who wants to chat with each other. But connected to different servers. UserA to Server1 and UserB to server2
Need some suggestion on how to implement
Approach 1 :
Initially, I was thinking to have a centralized database, which stores the connections(whenever a connection is established). Something like (simplest table structure)
User_id , Socket_object
Let's say userA is sending a message to userB.
So that I will fetch the socket object of the receiver(userB) from the database and send the message directly to UserB . But later I came to know ServerSocket object is not serializable in Java.
Approach 2:
Whenever a connection is established, the server which accepted the connection can save the below details in a table.
user_id , server_name , server_port
when some server receives a message. It gets the destination server detail from the table and make a connection(probably HTTP, since we don't need persistence) to that destination server and push the message. Then the destination server passes the information to the respective user.
How to proceed with Implementation? Is there any other better way to implement it.
Please do not suggest some third-party libraries.