What should I be using? Socket.io rooms or Redis pub-sub?

Josh Mc picture Josh Mc · Feb 18, 2013 · Viewed 24.9k times · Source

Pretty simple question. I am building a realtime game using nodejs as my backend and I am wondering if there is any information available on which one is more reliable and which one is more efficient? I am heavily using both Redis and Socket.io throughout my code. So I want to know whether I should be utilizing Socket.io's Rooms or I would be better off using redis' pub-sub ?

Update: Just realized there is a very important reason why you may want to use redis pub/sub over socket.io rooms. With Socket.io rooms when you publish to listeners, the (browser)clients recieve the message, with redis it is actually the (redis~on server)clients who recieve messages. For this reason, if you want to inform all (server)clients of information specific to each client and maybe do some processing before passing on to browser clients, you are better off using redis. Using redis you can just fire off an event to generate each users individual data, where as with socket.io you have to actually generate all the users unique data at once, then loop through them and send them their individual data, which almost defeats the purpose of rooms, at least for me.

Unfortunately for my purposes I am stuck with redis for now.

Update 2: Ended up developing a plugin to use only 2 redis connections but still allow for individual client processing, see answer below....

Answer

Pascal Belloncle picture Pascal Belloncle · Feb 19, 2013

Redis pub/sub is great in case all clients have direct access to redis. If you have multiple node servers, one can push a message to the others.

But if you also have clients in the browser, you need something else to push data from a server to a client, and in this case, socket.io is great.

Now, if you use socket.io with the Redis store, socket.io will use Redis pub/sub under the hood to propagate messages between servers, and servers will propagate messages to clients.

So using socket.io rooms with socket.io configured with the Redis store is probably the simplest for you.