0
votes

I've written a small Socket.IO server, which works fine, I can connect to it, I can send/receive messages, so everything is working ok. Just the relevant part of the code is presented here:

var RedisStore = require('socket.io/lib/stores/redis');
const pub = redis.createClient('127.0.0.1', 6379);
const sub = redis.createClient('127.0.0.1', 6379);
const store = redis.createClient('127.0.0.1', 6379); 

io.configure(function() {
  io.set('store', new RedisStore({
    redisPub    : pub, 
    redisSub    : sub, 
    redisClient : store
  }));
});

io.sockets.on('connection', function(socket) {
 socket.on('message', function(msg) {
    pub.publish("lobby", msg);
  });


  /*
   * Subscribe to the lobby and receive messages.
   */
  var sub = redis.createClient('127.0.0.1', 6379);
  sub.subscribe("lobby");
  sub.on('message', function(channel, msg) {
    socket.send(msg);
  });
});

Here, I'm interested in the problem where certain client is subscribed to a different room, which is why I'm also using the sub Redis variable inside each socket connection: because each client can be subscribed to a different room and can receive messages from there. I'm not entirely sure whether the code above is ok, so please let me know if I need to do anything else than define the sub Redis connection inside the Socket.IO connection: this also means that a new Redis connection is spawned for each client connecting serving his messages from the subsribed room? I guess this is quite an overhead, so I would like to solve it anyway possible?

Thank you

1

1 Answers

0
votes

Both node.js and redis are very good at handling lots of connections (thousands is no problem), so what you're doing is fine.

As a side note, you will want to look into upping your file descriptor limits if you do intend on supporting thousands of connections.