I can get room's clients list with this code in socket.io 0.9.
io.sockets.clients(roomName)
How can I do this in socket.io 1.0?
I can get room's clients list with this code in socket.io 0.9.
io.sockets.clients(roomName)
How can I do this in socket.io 1.0?
For those of you using namespaces I made a function too that can handle different namespaces. It's quite the same as the answer of nha.
Using @ryan_Hdot link, I made a small temporary function in my code, which avoids maintaining a patch. Here it is :
If using a namespace :
Which I use as a temporary fix for
io.sockets.clients(roomId)
which becomesfindClientsSocketByRoomId(roomId)
.EDIT :
Most of the time it is worth considering avoiding using this method if possible.
What I do now is that I usually put a client in it's own room (ie. in a room whose name is it's clientID). I found the code more readable that way, and I don't have to rely on this workaround anymore.
Also, I haven't tested this with a Redis adapter.
If you have to, also see this related question if you are using namespaces.
You can see this github pull request for discussion on the topic, however, it seems as though that functionality has been stripped from the 1.0 pre release candidate for SocketIO.
Consider this rather more complete answer linked in a comment above on the question: https://stackoverflow.com/a/24425207/1449799
The clients in a room can be found at
This is an associative array with keys that are socket ids. In our case, we wanted to know the number of clients in a room, so we did
Object.keys(io.nsps[yourNamespace].adapter.rooms[roomName]).length
In case you haven't seen/used namespaces (like this guy[me]), you can learn about them here http://socket.io/docs/rooms-and-namespaces/ (importantly: the default namespace is '/')
Updated (esp. for @Zettam):
checkout this repo to see this working: https://github.com/thegreatmichael/socket-io-clients
As of at least 1.4.5 nha’s method doesn’t work anymore either, and there is still no public api for getting clients in a room. Here is what works for me.
io.sockets.adapter.rooms[roomId]
returns an object that has two properties, sockets, and length. The first is another object that has socketId’s for keys, and boolean’s as the values:So my code to get clients looks like this: