2
votes

I am trying to setup websocket servers behind a load balancer. At first, I used the socket.io library. But I found that it requires sticky session when used behind a load balancer.

According to this website, it sends multiple requests to perform handshake and establish a connection. If the requests are sent to different servers, the connection will fail.

After further study, i found that other websocket server library like SockJS also have the same problem. They all require sticky session to work behind a load balancer.

Now I am checking the websocket library ws. But I could not find any example of using it behind load balancer.

Does the ws library requires sticky session to work?

Is there any other websocket library that can work without sticky session behind a load balancer?

1
I don't think so. You would need to replicate the socket sessions across all your servers, to avoid the sticky session .Hosar

1 Answers

2
votes

Is there a specific reason why you can't / don't want to rely on sticky sessions?

If you want to distribute socket connections across multiple hosts you are going to need some solution, and sticky sessions is a perfectly good one.

The socket.io page on using multiple nodes you link to even describes a way to implement the solution, "by routing clients based on their originating address" via NginX. Have you tried this and found that it doesn't work?

There is also a very good article on Horizontally Scaling Node.js and WebSockets with Redis which describes solving the exact issue you have with sticky sessions and automatic failover.