How can I scale socket.io?

2019-01-21 19:41发布

Let's say a server gets 10,000 concurrent connections (via socket.io). That's a lot, and if it can't handle any more, I need to spin up another server.

How can I sync the two servers together with their socket.io?

2条回答
别忘想泡老子
2楼-- · 2019-01-21 20:30

I wouldn't use Cluster to scale Socket.IO. Socket.IO 0.6 is designed as a single process server, and it uses long living connections or polling connections to achieve a real time connection between the server and client.

If you put Cluster infront of your socket.io client you will basically distribute the polling transports between different servers, who are not aware of the client. This will result in broken connections. But also broadcasting to all your clients will be a pain as they are all distributed on different servers and you don't have IPC between them.

So I would only advice to use Cluster if you only use Web Socket & Flash Socket connections and don't need to use the broadcast functionality.

So what should you do?

You could wait until socket.io 0.7 is released which is designed from the ground up to be used on multiple processes.

Or you can use pub/sub to send messages between different servers.

查看更多
冷血范
3楼-- · 2019-01-21 20:31

You can try to use for example cluster module and distribute the load to multiple cores (in case you have a multi-core CPU). In case this is not enough you can try to use reverse proxy for distributing requests across multiple servers and redis as a central session data store (if it's possible for your scenario).

查看更多
登录 后发表回答