Rojith.P
Rojith.P

Reputation: 33

Socket.io with load balancer

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

Upvotes: 0

Views: 45

Answers (1)

Kishan Sharma
Kishan Sharma

Reputation: 61

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.

  1. When a user connects, the server updates redis with an entry like userId -> serverId , just to ensure the system know which server the user is connected to.
  2. When user A sends a message to user B, the server handling user A queries Redis to find out which server B is connected to.
  3. Once the 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).
  4. The server hosting user B receives the message and delivers it to user B.

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.

Upvotes: 1

Related Questions