Horizontally scale socket.io with redis
Asked Answered
N

2

7

I currently am creating a horizontally scalable socket.io server which looks like the following:

                 LoadBalancer (nginx)

      Proxy1      Proxy2      Proxy3      Proxy{N}

 BackEnd1   BackEnd2   BackEnd3   BackEnd4   BackEnd{N}

My question is, with socket-io redis module, can I send a message to a specific socket connected to one of the proxy servers from one of the backend servers if they are all connected to the same redis server? If so, how do I do that?

Novia answered 11/12, 2015 at 6:10 Comment(0)
O
3

As you wan to scale socket.io server, and you have used nginx as load balancer, do not forget to setup sticky load balancing, othersie single connection will be connected to multiple server based on load balancer pass the connection to socket.io server. So better to use sticky load balancing

With the redis socket io adapter, you can send and receive message with one or more socket.io server with help of Redis Pub/Sub implementation.

if you tell me which technology is used for Proxy and Backend, i will let you know more information on this.

Ormiston answered 11/12, 2015 at 13:35 Comment(1)
All of the servers use Node.js, I'm just asking how I can emit to a client from one of the backend servers provided they're all connected to the same redis store.Novia
P
2

Using the socket.io-redis module all of your backend servers will share the same pool of connected users. You can emit from Backend1 and if a client is connected to Backend4 he will get the message.

The key for this working though with Socket.io is to use sticky sessions on nginx so that once I client connects, it stays on the same machine. This is because the way that socket.io starts with a WebSocket and several long polling threads, they all need to be on the same backend server to work correctly.

Instead of sticky sessions, you can change your client connection optons to use Websockets ONLY and this will remove the problems with the multiple connections to multiple servers as there will only be one connection, the single websocket. This will also make your app lose the ability to downgrade to long-polling instead of WebSockets.

Pollywog answered 15/12, 2015 at 20:23 Comment(1)
Hey thanks for the reply. For some reason, this still is not working for me. Can you please take a look at this thread and let me know the best way to proceed? #34325729Novia

© 2022 - 2024 — McMap. All rights reserved.