question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

socket.rooms is a useless object

See original GitHub issue

This is concerning the socket.rooms object, documented as:

A hash of strings identifying the rooms this client is in, indexed by room name.

I don’t know what that means, but the example is clear:

io.on('connection', function(socket){
  socket.join('room 237', function(){
    console.log(socket.rooms); // [ <socket.id>, 'room 237' ]
    io.to('room 237', 'a new user has joined the room'); // broadcast to everyone in the room
  });
});

When I run console.log(socket.rooms);, I want that pretty array (so I can slice off <socket.id> and get just the rooms), but I get this ugly object back:

io.on('connection', function(socket){
  socket.join('room 237', function(){
    console.log(socket.rooms); // { <socket.id>: '<socket.id>', 'room 237': 'room 237' }
  });
});

… which I then have to turn into an array with .keys() and then pick out the socket.id by hand (since it might not be first).

Is this a bug? I’m using 1.7.3.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:9
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

7reactions
chargingsleipnircommented, Mar 16, 2018

Yah this seems unnecessarily complicated. I’m really struggling to understand why the user’s socket id constitutes being a “room”?, or why key:value pairs are needed. It just needs to be a list - whatever rooms are present, you are in, done.

['room1', 'room2', 'room3']

And as above, io.rooms to get them all, either, also as a string array, or in this case as an object with room names as keys and an array of socket ids as their value.

{ room1: ['id1', 'id2', 'id3'], room2: ['id4', 'id5', 'id6'] }

1reaction
StefansAryacommented, Jun 11, 2018

I think the main reason is the lookup performance. For the example, when a user was joined 100 room it would be more faster to use object lookup to find out if he was joined a room rather than iterating array value.

Refer to my answer on stackoverflow

{
    "room1":"room1",
    "room2":"room2",
    ...
    "room100":"room100"
}

To check if user has joined a room just a simple like this: if(socket.rooms[roomID]) return true;

If it’s array type then we need to use indexOf or iterate every array: if(socket.rooms.indexOf(roomID) != -1) return true;

But I totally agree if the the key value is changed to a boolean: { room1: true, ... , room100: true }

It may help saving some memory usage

Read more comments on GitHub >

github_iconTop Results From Across the Web

Socket.io what room(s) is the socket in? - Stack Overflow
rooms" in the console log, I get [Object object]. It is some type of object, but without any documentation on how to access...
Read more >
Learn Socket.io In 30 Minutes - YouTube
Socket.io is an amazing library for client/server communication, but it can be a bit confusing to get started with. In this video I...
Read more >
Rooms | Socket.IO
A room is an arbitrary channel that sockets can join and leave. It can be used to broadcast events to a subset of...
Read more >
Soul Locket - Binding of Isaac: Rebirth Wiki - Fandom
Soul Locket is an unlockable passive item added in The Binding of Isaac: Repentance. Gives Isaac one of the following stat boosts per...
Read more >
GOSF – Go Socket.IO Framework
IO v2 Support; Request/Response Acknowledgement-Based Cycle; Broadcast/Room Support; Microservices Using Socket.IO; Plugable Architecture; App, Client, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found