Closed synzhu closed 4 years ago
The clients()
method works with the in()/to()
modifier:
io.in('room1').clients((error, clients) => {
console.log(clients); // => [Anw2LatarvGVVXEIAAAD]
});
Reference: https://socket.io/docs/server-api/#namespace-clients-callback
If you are using the default in-memory adapter, io.clients()
(without any room) will indeed produce the same result as Object.keys(namespace.connected)
.
If you are using the redis adapter, Object.keys(namespace.connected)
will only contain the socket ids on a given server, while io.clients()
will return the list of all socket ids across each server.
I'm a bit confused about what the right way is to obtain a list of sockets connected to a room. In particular, are there any scenarios in which
Object.values(namespace.connected)
would be different from the list of client ID's returned bynamespace.clients
? It seems that the first method returns a map of client ID to socket objects, and the second accepts a callback function which is called with the list of client ID's. If I wanted to obtain the list of sockets objects connected to a room or simply the number of clients connected to the room, what is the best way to obtain this?