I start building chat server using Socket.io with multiple nodes. It uses Socket.io-redis to connect all servers together and rooms for messaging.
When a client connects with server I join client to some room.
io.on('connection', function(socket){
socket.join("CLIENT_1");
});
So I want to get number of clients connected to room "CLIENT_1"
,
io.sockets.adapter.rooms["CLIENT_1"];
but I only get connection from current process. How can I get connection from all server processes connected through the redis adapter?
I have gone through this question:
How to check socket is alive (connected) in socket.io with multiple nodes and socket.io-redis
but it didn't help me.
Thanks for advance.
my socket.io version 1.3.5on('create or join', function (numClients, room) { socket. join(room); }); I use this code for get clients in room : console.
The Redis adapter extends the broadcast function of the in-memory adapter: the packet is also published to a Redis channel (see below for the format of the channel name). Each Socket.IO server receives this packet and broadcasts it to its own list of connected sockets.
Server was able to handle 100000 events distributed in 1000 rooms with 5 requests per second querying db.
socket-io. client is the code for the client-side implementation of socket.io. That code may be used either by a browser client or by a server process that is initiating a socket.io connection to some other server (thus playing the client-side role in a socket.io connection).
As of this writing:
The redis adapter extends the base adapter, but it only overrides/adds the following properties:
onmessage
broadcast
add
del
delAll
With this code of yours:
io.sockets.adapter.rooms["CLIENT_1"];
you are querying the rooms
property. This wasn't overridden by the redis adapter, so you're actually querying the base adapter, which only knows about rooms/clients in the current process.
Why didn't the redis adapter override the rooms
property? Because in order to match the exact call signature above, it would have to query the redis instance to construct an object containing all rooms and connections every time the property is accessed. Not good. (That is unless you can figure out how to compute object values at the time their values are queried.)
If you want to get the number of connections to the "CLIENT_1"
room across all processes in the cluster, you'll have to add that functionality to the adapter itself with a method like this:
/**
* Count the number of connections in a room.
*
* @param {String} room id
* @param {Function} callback (optional)
* @api public
*/
Redis.prototype.numClients = function(room, fn){ ... }
wherein you'll query the redis db instance.
IMO, this should be part of the base adapter interface for all other adapters to implement. It's a common problem.
This method works perfectly:
io.sockets.adapter.clients(["room1"], function(err, clients){
console.log("total clients in room1: %d", clients.length);
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With