1

I am using version 2.3.0, and I want to get a list of socket objects that have joined a certain room. Now I know that there have been many similar questions in the past, and from what I've gathered, the following code can be used to obtain a list of the socket id's in a given room (correct me if I'm wrong):

io.of('/').in('room_name').clients((error, clients) => {
  if (error) throw error;
  // Returns an array of client IDs like ["Anw2LatarvGVVXEIAAAD"]
  console.log(clients); 
});

From these client IDs, it looks like I can get the actual socket objects by using io.of('/').in('room_name').connected[id]?

What I don't understand is, what does io.of('/').in('room_name').connected actually contain? The documentation does not make it super clear, as it seems to suggest that it is a map of the socket objects connected to that room. However, if this were the case, couldn't I simply get the list of socket objects like so:

const sockets = Object.values(io.of('/').in('room_name').connected)

There have been many questions on this in the past (for example), and none of them seem to mention this. Does connected not actually contain what I think it does? Or is this only relevant when you are using multiple Socket.IO nodes?

shmth
  • 458
  • 3
  • 11
  • I tested this code and it seems to work...What is the problem exactly? Maybe you're using an older version? – i.brod Apr 13 '20 at 11:43
  • @i.brod I wasn't saying it doesn't work, I was just wondering why have two separate API's for the same thing? Unless they are not actually the same? – shmth Apr 14 '20 at 08:54
  • It's possible that different api's exist for backwards compatibility i guess..Socketio seems to have changed a lot, during the years. You can see so many "Edit February 2019:" sections in the various posts about these issues. – i.brod Apr 14 '20 at 09:08

0 Answers0