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?
The short answer:
I analysed
io
:I got the following output:
after joining room "public-alfred" from a single client
io.sockets.adapter.rooms
contained:In a new version of socket.io (1.x),
io.sockets.manager.rooms
will cause an error. You should useio.sockets.adapter.rooms
instead.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:
This would help so that if a client join a room by:
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.
in case you are using
socket.io 2.1.0
and chanced upon this.
in 2.1.0, all the rooms in the server are located in the variable io.sockets.adapter.rooms
You will get the following results.
If
io.sockets.adapter
isundefined
..I'm using socket.io 1.7.1 (Server).
It seems
io.of("......").sockets.adapter
isundefined
on that version ofsocket.io
. So I usedio.adapter.rooms
instead and it works.As everyone said, in a new version of socket.io (1.x) the rooms can be found at:
This will return something like:
The only room that I want to get is 'myRoom', so I wrote the following function for doing that:
This was kind of confusing for me, hope this helps!