Is it possible to get rooms which socket is currently in, without calling
io.sockets.clients(roomName)
for every room name and looking for this socket in results
socket.io rooms are a lightweight data structure. They are simply an array of connections that are associated with that room. You can have as many as you want (within normal memory usage limits). There is no heavyweight thing that makes a room expensive in terms of resources.
In socket.io version 1+ the syntax is:
socket.rooms
var rooms = Object.keys(io.sockets.adapter.sids[socket.id]); // returns [socket.id, 'room-x'] or [socket.id, 'room-1', 'room-2', ..., 'room-x']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With