Question

I have an nodejs chat app where multiple clients connect to a common chat room using socketio. I want to scale this to multiple node processes, possibly on different machines. However, clients that connect to the same room will not be guaranteed to hit the same node process. For example user 1 will hit node process A and user 2 will hit node process B. They are in the same room so if user 1 sends a message, user 2 should get it. What's the best way to make this happen since their connections are managed by different processes?

I thought about just having the node processes connect to redis. This at least solves the problem that process A will know there's another user, user 2, in the room but it still can't send to user 2 because process B controls that connection. Is there a way to register a "value changed" callback for redis?

I'm in a server environment where I can't control any of the routing or load balancing.

Was it helpful?

Solution

Both node.js processes can be subscribed to some channel through redis pub/sub and listen to messages which you pass to this channel. For example, when user 1 connects to process A on the first machine, you can store in redis information about this user along with the information which process on which machine manages it. Then when user 2, which is connected to process B on the second machine, sends a message to user 1, you can publish it to this channel and check which process on which machine is responsible for managing communication with user 1 and respond accordingly.

OTHER TIPS

I have done(did) some research on this. Below my findings:

Like yojimbo87 said you first just use redis pub/sub(is very optimized).

http://comments.gmane.org/gmane.comp.lang.javascript.nodejs/22348

Tim Caswell wrote:

It's been my experience that the bottleneck is the serialization and de-serialization of the data, not the actual channel. I'm pretty sure you can use named pipes, but I'm not sure what the API is. msgpack seems like a good format for the data interchange. There are a few libraries out there that implement msgpack or ipc frameworks on top of it.

But when serialization / deserialization becomes your bottle-neck I would try to use https://github.com/pgriess/node-msgpack. I would also like to test this out, because I think the sooner you have this the better?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top