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?