28

I am trying to scale a simple socket.io app across multiple processes and/or servers.

Socket.io supports RedisStore but I'm confused as to how to use it.

I'm looking at this example, http://www.ranu.com.ar/post/50418940422/redisstore-and-rooms-with-socket-io

but I don't understand how using RedisStore in that code would be any different from using MemoryStore. Can someone explain it to me?

Also what is difference between configuring socket.io to use redisstore vs. creating your own redis client and set/get your own data?

I'm new to node.js, socket.io and redis so please point out if I missed something obvious.

2
  • Here's a code snip using RedisStore with node you may find interesting; it doesn't talk differences, though; I was, ironically, just reading on this topic :)
    – Kato
    Feb 15, 2012 at 17:26
  • The links for these dont work anymore =/
    – qodeninja
    Jan 23, 2014 at 0:05

4 Answers 4

27

but I don't understand how using RedisStore in that code would be any different from using MemoryStore. Can someone explain it to me?

The difference is that when using the default MemoryStore, any message that you emit in a worker will only be sent to clients connected to the same worker, since there is no IPC between the workers. Using the RedisStore, your message will be published to a redis server, which all your workers are subscribing to. Thus, the message will be picked up and broadcast by all workers, and all connected clients.

Also what is difference between configuring socket.io to use redisstore vs. creating your own redis client and set/get your own data?

I'm not intimately familiar with RedisStore, and so I'm not sure about all differences. But doing it yourself would be a perfectly valid practice. In that case, you could publish all messages to a redis server, and listen to those in your socket handler. It would probably be more work for you, but you would also have more control over how you want to set it up. I've done something similar myself:

// Publishing a message somewhere
var pub = redis.createClient();
pub.publish("messages", JSON.stringify({type: "foo", content: "bar"}));

// Socket handler
io.sockets.on("connection", function(socket) {
  var sub = redis.createClient();
  sub.subscribe("messages");
  sub.on("message", function(channel, message) {
    socket.send(message);
  });

  socket.on("disconnect", function() {
    sub.unsubscribe("messages");
    sub.quit();
  });
});

This also means you have to take care of more advanced message routing yourself, for instance by publishing/subscribing to different channels. With RedisStore, you get that functionality for free by using socket.io channels (io.sockets.of("channel").emit(...)).

A potentially big drawback with this is that socket.io sessions are not shared between workers. This will probably mean problems if you use any of the long-polling transports.

3

I set up a small github project to use redis as datastore.

Now you can run multiple socket.io server processes.

https://github.com/markap/socket.io-scale

1
  • seems outdated, RedisStore is not available anymore in socket.io
    – Flion
    Aug 4, 2016 at 8:20
2

Also what is difference between configuring socket.io to use redisstore vs. creating your own redis client and set/get your own data?

The difference is that, when you use 'RedisStore', the socket.io itself will save the socket heartbeat and session info into the Redis, and if you use cluster with node.js, the user client can work.

Without redis, the client might change the node.js process next time, so the session will be lost.

0

The difference is, if you have a cluster of node.js instances running, memStore won't work since it's only visible to a single process.

2
  • 1
    And what would be the solution?
    – nalply
    Oct 19, 2012 at 22:39
  • Working on this right now-- here's what I did for a socket store (IPC) and session store (that you can share with another server... like Express!) We're testing this in a clustered setup now-- I'll post back here if I see any problems. github.com/balderdashy/sails/tree/master/session_experiment May 8, 2013 at 7:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.