I am working on abc project by using websocket. we are using load balance for our application deployed by PM2 so i am using just in-memory for storing socket connection like that.
Map<number,Socket.socket>={}
;
How to use efficient way for storing sockets and load balancer?
it's one of the challenging type i think.
Use sockets with load balancer
So to ensure that users are consistently connected to the same server, you can configure the load balancer to use sticky sessions, which involves assigning a cookie or unique id to each user when they connect, which allows the load balancer to always router their requests to the same server.
Now if user A is connected to one server and user B is connected to another, and user A wants to send a message to user B, you can use a Redis cluster to manager user-to-server mappings.
userId -> serverId
, just to ensure the system know which server the user is connected to.serverId
for user B is identified, the server hosting A can send the message to the appropriate server using inter-server communication (eg websockets, HTTP or Redis Pub/Sub).Using this method you can horizontally scale your socket server and i guess the method you're using here Map<number,Socket.socket>={}
is best for node js based application but remember it just contains the user connected with a single server and each server will have it's own Map for connected clients.