node.js - Socket.io: How to count clients in a room with Socket.io-redis adapter -
i start building chat server using socket.io multiple nodes. uses socket.io-redis connect servers , rooms messaging.
when client connects server join client room.
io.on('connection', function(socket){ socket.join("client_1"); }); so want number of clients connected room "client_1",
io.sockets.adapter.rooms["client_1"]; but connection current process. how can connection server processes connected through redis adapter?
i have gone through question:
how check socket alive (connected) in socket.io multiple nodes , socket.io-redis
but didn't me.
thanks advance.
as of writing:
the redis adapter extends base adapter, overrides/adds following properties:
onmessagebroadcastadddeldelall
with code of yours:
io.sockets.adapter.rooms["client_1"]; you querying the rooms property. this wasn't overridden redis adapter, you're querying base adapter, knows rooms/clients in current process.
why didn't redis adapter override rooms property? because in order match exact call signature above, have query redis instance construct object containing rooms , connections every time property accessed. not good. (that unless can figure out how compute object values @ time values queried.)
if want number of connections "client_1" room across processes in cluster, you'll have add functionality the adapter itself method this:
/** * count number of connections in room. * * @param {string} room id * @param {function} callback (optional) * @api public */ redis.prototype.numclients = function(room, fn){ ... } wherein you'll query redis db instance.
imo, should part of base adapter interface other adapters implement. it's common problem.
Comments
Post a Comment