35

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?

2

5 Answers 5

36

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

io.nsps[yourNamespace].adapter.rooms[roomName]

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

4
  • 1
    Am I "this guy" ? ^^ Also, the default namespace is "/" or "".
    – nha
    Oct 13, 2014 at 19:48
  • @nha sorry, no, I'm "this guy"
    – Michael
    Oct 13, 2014 at 23:40
  • @Michael I see :) To be honest it could have been me as well, I never really used namespaces (except for testing).
    – nha
    Oct 13, 2014 at 23:47
  • @Zettam yes. I updated the answer with a link to an example in github. See if that one works for you.
    – Michael
    Nov 10, 2014 at 17:15
11

Using @ryan_Hdot link, I made a small temporary function in my code, which avoids maintaining a patch. Here it is :

function getClient(roomId) {
  var res = [],
      room = io.sockets.adapter.rooms[roomId];
  if (room) {
    for (var id in room) {
      res.push(io.sockets.adapter.nsp.connected[id]);
    }
  }
  return res;
}

If using a namespace :

function getClient (ns, id) {
  return io.nsps[ns].adapter.rooms[id]
}

Which I use as a temporary fix for io.sockets.clients(roomId) which becomes findClientsSocketByRoomId(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.

7

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.

function get_users_by_room(nsp, room) {
  var users = []
  for (var id in io.of(nsp).adapter.rooms[room]) {
    users.push(io.of(nsp).adapter.nsp.connected[id]);
  };
  return users;
};
6

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:

Room {
   sockets: 
   { '/#vQh0q0gVKgtLGIQGAAAB': true,
     '/#p9Z7l6UeYwhBQkdoAAAD': true },
   length: 2 }

So my code to get clients looks like this:

var sioRoom = io.sockets.adapter.rooms[roomId];
if( sioRoom ) { 
  Object.keys(sioRoom.sockets).forEach( function(socketId){
    console.log("sioRoom client socket Id: " + socketId );
  }); 
}   
1
  • Thank you for sharing your trick. Your answer is now the real answer with up to date socket.io.
    – MacKentoch
    Mar 5, 2017 at 19:19
5

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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.