Skip to content

how to listen for a event only in specific rooms not globally. In the docs I couldn't found a way to do that! #3951

Answered by darrachequesne
arnav-kr asked this question in Q&A
Discussion options

You must be logged in to vote

Hi! I see two possible solutions:

  • you can check if the socket is actually in the restricted room
io.on("connection", (socket) => {
  socket.on("my-event", () => {
    const isAllowed = socket.rooms.has("admin");
    if (isAllowed) {
      // ...
    }
  });
});
  • or use a namespace (that's their exact purpose)
io.of("/admin").on("connection", (socket) => {
  socket.on("my-event", () => {
    // ...
  });
});

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@arnav-kr
Comment options

@arnav-kr
Comment options

Answer selected by arnav-kr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants