forJ
forJ

Reputation: 4617

Multi threading nodeJS and Socket IO

Okay so multithreading nodeJS isn't much problem from what I've been reading. Just deploy several identical apps and use nginx as reverse proxy and load balancer of all the apps.

But actually native cluster module works pretty well too, I found.

However, what if I have socket.io with the nodeJS app? I have tried the same strategy with nodeJS + socket.IO; however, it obviously did not work because every socket event emitted will be more or less evenly distributed and sockets other than the one that made the connection would have no idea where the request came from.

So best method I can think of right now is to separate nodeJS server and socket.IO server. Scale nodeJS server horizontally (multiple identical apps) but just have one socket.IO server. Although I believe it would be enough for the purpose of our solution, I still need to look out for future. Has anyone succeeded in horizontally scaling Socket.IO? So multiple threads?

Upvotes: 1

Views: 2755

Answers (1)

David Alsh
David Alsh

Reputation: 7609

The guidelines on the socket.io website use Redis with a package called socket.io-redis

https://socket.io/docs/using-multiple-nodes/

Looks like is just acts like a single pool for the connections, and each node instance connects to that.

Putting your socket server on a separate service (micro-service) is a probably fine, the downside is needing to manage communications between the two instances.

Upvotes: 1

Related Questions