How to run WebSockets with multiple server instances?

It is useful to run multiple instances of HTTP servers to scale.

However, it seems that this will not work with WebSockets, because each server instance will have its own set of client connections.

How can you run WebSockets on multiple server instances if they all need to share a common set of connections?

+6
source share
3 answers

I was looking for the same thing to develop a scaling-ready web application, and apparently there is not much discussion about this article.

Case 1 The recommended approach is to use the Redis cache, for example Heroku , use the service bus or store in db, like the 3 approaches mentioned in Scaleout in SignalR . However, there is a trick, there should be a little latency , because in order for the second instance to want to know, the scheduler or desktop / desktop service must be executed continuously to detect new changes in the cache / queue / db .

Case 2 In my case, I want a high frequency almost in real time . Therefore, it must have a permanent connection between instances. However, the challenge is, we cannot do this between a stateless web server.

Thus, we still need an intermediate multi-year background / working service and implement our own net / socket / TCP code from the code and maintain the connection pool, but this is not an easy task.

I’m thinking about using the Websocket client on the server (for this service), so the pattern for communicating with the client and between instances is almost the same. See this: 1 2 . It remains only to update the IP address of the instance in a distributed cache or db on each instance up / down.

The last option is to explore the new WebRTC

+1
source

I have not tried it yet, but I am thinking that sticky compounds can solve the problem. You can set this parameter in the load balancer.

+1
source

The best solution I've found so far is http://pusher.com/ .

Easy setup. Great documentation.

0
source

All Articles