Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Good way to use socket.io with cluster in multi-core server?

I have created a nodejs application which works fine in a single core. This application uses socket.io for light&fast communication.

Now, I wanted to scale my application vertically, by enabling it to run on multi-core system using cluster module in nodejs.

Everything in my application stores data in Redis, therefore, there isn't a problem dealing with some basic data & sessions.

However, when I spawn multiple worker processes using cluster, it seems that each worker has its own socket handling.

For example, lets assume there is a chat room called 'guest' room.

User A and user B connects to the room, and they are distributed to the different worker process.

Since they are in different processs and these processes do not share sockets listener, there is no way for user A and user B to talk to each other.

What is a good approach to solve this problem? Does socket.io supports multi-core system?

Is socket.io only for single core use?

like image 501
user482594 Avatar asked Jul 01 '12 16:07

user482594


1 Answers

You could use socket.io as normal but with a redis store in the background. It will also support multiple instances on socket.io without any external library. It even supports rooms over multiple instances.

Link to how to set up socket.io with redis: Using Multiples Nodes/Processes with socket.io

like image 169
Martin Avatar answered Oct 24 '22 07:10

Martin