Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

There doesn't appear to be a way to track room creation/deletion #2897

Closed
1 of 2 tasks
vincentwoo opened this issue Mar 14, 2017 · 6 comments
Closed
1 of 2 tasks

There doesn't appear to be a way to track room creation/deletion #2897

vincentwoo opened this issue Mar 14, 2017 · 6 comments
Milestone

Comments

@vincentwoo
Copy link

You want to:

  • report a bug
  • request a feature

Current behaviour

Right now, it appears that rooms are a socket-level construct only. Individual connections appear to know what rooms they're a part of. Adapters like socket.io-redis appear to have just enough functionality to know how to route messages to all members of a room.

Expected behaviour

In many applications, socket.io will sit on top of a room that represents some potentially costly resource. For instance, in my use case, I want to spin up a docker container to be used by everyone in a room. That means I need to know when to create one and when to delete one when it's no longer being used.

I can try to track room membership on my own, but then I become a central point of failure anyhow.

Suggestion

In a system like the one socket.io-redis suggests, I think it should be possible for all nodes to subscribe to when rooms are created or destroyed, since those changes should be pubbed through redis already.

@darrachequesne
Copy link
Member

Well, I guess we could emit an event when a room is created:

https://github.com/socketio/socket.io-adapter/blob/1.1.0/index.js#L56

for (var i = 0; i < rooms.length; i++) {
    var room = rooms[i];
    this.sids[id] = this.sids[id] || {};
    this.sids[id][room] = true;
    if (!this.rooms.hasOwnProperty(room)) {
      this.rooms[room] = Room();
      this.emit('create-room', room);
    }
    this.rooms[room].add(id);
}
// and then
io.of('/').adapter.on('create-room', /* */);

What do you think?

@vincentwoo
Copy link
Author

I think that would certainly work for detecting creation, and in the basic socket.io-adapter that might be okay. I think the bigger questions are how you would do it in the redis model, as well as detecting deletion. For the basic adapter you could add a corresponding emit here: https://github.com/socketio/socket.io-adapter/blob/c6f7baec3b000bf75ce4b5278ac146dc993a537f/index.js#L81.

@throwarray
Copy link

Could I suggest join and leave events as well? The current callback approach isn't ideal and I notice a lot of people rolling their own room implementations.

darrachequesne added a commit to socketio/socket.io-adapter that referenced this issue Jan 15, 2021
The adapter will now emit the following events:

- create-room (arg: room)
- delete-room (arg: room)
- join-room (args: room, sid)
- leave-room (args: room, sid)

Related: socketio/socket.io#2897
darrachequesne added a commit to socketio/socket.io-website that referenced this issue Feb 19, 2021
@darrachequesne
Copy link
Member

For future readers: starting with socket.io@3.1.0, the underlying Adapter will emit the following events:

  • create-room (argument: room)
  • delete-room (argument: room)
  • join-room (argument: room, id)
  • leave-room (argument: room, id)

Example:

io.of("/").adapter.on("create-room", (room) => {
  console.log(`room ${room} was created`);
});

io.of("/").adapter.on("join-room", (room, id) => {
  console.log(`socket ${id} has joined room ${room}`);
});

Documentation: https://socket.io/docs/v3/rooms/#Room-events

@darrachequesne darrachequesne added this to the 3.1.0 milestone Feb 19, 2021
@Tsourdox
Copy link

Tsourdox commented May 2, 2022

Is there a way to get the correct typescript types for these events?

@darrachequesne
Copy link
Member

@Tsourdox no, this is not currently possible, but we could add this I guess 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants