How to iterate through a list of sockets at a specific namespace in a specific room? #5130
Answered
by
darrachequesne
jasper-clarke
asked this question in
Q&A
-
Below here is code from the private messaging example. My problem is I need to be able to do the below but only for a specific room, being the one that the user is connected to. How can I do this? let users = [];
for (let [id, socket] of io.of("/").sockets) {
users.push({
userID: id,
username: socket.username,
message: "",
});
} |
Beta Was this translation helpful? Give feedback.
Answered by
darrachequesne
Jul 11, 2024
Replies: 1 comment
-
You can use the const sockets = await io.in("my-room").fetchSockets(); Reference: https://socket.io/docs/v4/server-api/#namespacefetchsockets |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
jasper-clarke
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can use the
fetchSockets()
methods:Reference: https://socket.io/docs/v4/server-api/#namespacefetchsockets