Socket.io: How to get all the rooms

Created on 21 Nov 2014  ·  9Comments  ·  Source: socketio/socket.io

1630 already adds support for .clients([room], fn); to grab a list of socket ids in a room. I sent a PR to @FREEZX for adding the method .roomClients([socketid],fn) as well to grab a list of rooms the socket id is in.

This was possible in 0.9.x with io.sockets.manager.roomClients[socket_id] and it used to work across multiple nodes when used with RedisStore.

It was also possible to get a hash of all the rooms with io.sockets.manager.rooms but I don't see this available in 1.x. There is io.sockets.adapter.rooms but that doesn't work across nodes as far as I can see.

It is possible to get this data using the above two functions. First get all socket ids in room '' then call roomClients repeatedly to get the rooms and merge those but it means making n calls to roomClients, where as it was as simple as reading the io.sockets.manager.rooms object in 0.9.x

enhancement

Most helpful comment

i use io.sockets.adapter.rooms

All 9 comments

:+1:

+1 on this one. You can't even make a decent chat system without the ability to list rooms.

(Y)

:+1:

Does anyone have a solution to getting access to the all rooms?

Closed by https://github.com/socketio/socket.io-redis/pull/168.

io.adapter.allRooms(function (err, rooms) {
  console.log(rooms); // an array containing all rooms (accross every node)
});

I'm using 2.0.4 and io.adapter.allRooms not work. Any idea?

i use io.sockets.adapter.rooms

@wangdalang can you please show code example and link to docs for that. I can't seem to find it.

Was this page helpful?
0 / 5 - 0 ratings