how to list rooms on socket.io nodejs server

Viewed 60755

after progress on the question how to create socket.io multicast groups, I found making rooms a great way to do what I needed.

However, it would be great to know about all the rooms, without an extra data-structure.

Is it possible to get a list of all rooms on the server from the server socket?

14 Answers

In socketIO version 2.2.0,

  • io.sockets.manager does not exist anymore

You can get all sockets in a specific room using followings 3.

socket.adapter.rooms;
io.sockets.adapter.rooms
io.sockets.adapter.sids

If you are using namespace you should use socket.adapter.rooms to get the room lists.

{ '/userSocket#oXorUiql1GF68dbTAAAB':
   Room {
     sockets: { '/userSocket#oXorUiql1GF68dbTAAAB': true },
     length: 1 
     },
  roomOneName:
   Room {
     sockets: { '/userSocket#oXorUiql1GF68dbTAAAB': true },
     length: 1 
     },
  roomTwoName:
   Room {
     sockets: { '/userSocket#oXorUiql1GF68dbTAAAB': true },
     length: 1 
   } 
}
Object.keys(io.sockets.adapter.rooms)

use this to get all rooms.

in Socketio 2.0

to get rooms use

Object.keys(socket.adapter.rooms);

to get the room name

Object.keys(socket.adapter.rooms)[1];

Filtering out the 'real' rooms can be done like this:

var realRooms = Object.keys(io.sockets.adapter.rooms).reduce((filtered, key) => {
    if(!io.sockets.adapter.rooms[key].sockets.hasOwnProperty(key)) filtered.push(key);
    return filtered;
}, []);

According to the documentation of Socket.io-v3.x, to get all current rooms created, you just simply do socket.rooms

io.on('connection', socket => {

  console.log(socket.rooms);
  socket.on('disconnect', () => {
    // socket.rooms.size === 0
  });
});

and it return like this

Set(2) {
  '_y_xQeeF6PpFiPQqAAFC',
  '6113cb71359644408895afcf6112df3c76981e3f88bc5c08'
} 

I don't have enough reputation to post my comment to the original question, but in addition, if you want to list all the rooms in a namespace, it should be coded like this:

var nsp = io.of('/my-nsp');

var rooms = nsp.adapter.rooms;

This would help so that if a client join a room by:

socket.join('my-room');

You should be able to see your room name when you do console.log(rooms);

I just want to answer an unanswered comment above as I ran into the same issue after I saw mismatched room names.

function ActiveRooms(){
        var activeRooms = [];
        Object.keys(io.sockets.adapter.rooms).forEach(room=>{
            var isRoom = true;
            Object.keys(io.sockets.adapter.sids).forEach(id=>{
                isRoom = (id === room)? false: isRoom;
            });
            if(isRoom)activeRooms.push(room);
        });
        return activeRooms;}

on v4 proceed like this:

Array.from(io.adapter.rooms.entries())

It will return an array with the keys (roomIds) and values (socketIds), same structure as Object.entries():

[
  [roomId],[Set(2) {"socketId1","socketId2"}],
  [roomId2], [Set(3) {"socketId3","socketId4","socketId5"}],
]
Related