I’m using socket.io and Redis together with node.js – code below.
I’m definitely being stupid here, but I can’t understand why the Redis event only fires with the first user who connects.
I see the NEW CONNECTION console message again for each user who opens the page, but I only see the REDIS console message with the first user.
io = io.listen(app);
client.flushall();
io.sockets.on('connection', function (socket) {
// Update Redis with information about this connection.
console.log('*************** NEW CONNECTION');
client.multi()
.hmset("user:" + user_id, "nickname", nick)
.sadd("chatroom:" + room_id, user_id)
.exec();
// Check that the Redis record was set.
client.smembers("chatroom:" + room_id, function (err, data) {
if (err) { console.log(err); return; }
console.log('REDIS CALL FOR chatroom:' + room_id, data);
});
});
Do I need to create a separate Redis instance inside io.sockets.on for each client – and if so, will this scale?
Or otherwise, what am I doing wrong?
Got it! I was being stupid.
I was calling
client.quit()insidesocket.on('disconnect'...)– which was obviously deleting the Redis client altogether. DUH.Hope this helps someone else in the future, anyway.