-
Notifications
You must be signed in to change notification settings - Fork 10.1k
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
Comments
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? |
I think that would certainly work for detecting creation, and in the basic |
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. |
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
For future readers: starting with
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 |
Is there a way to get the correct typescript types for these events? |
@Tsourdox no, this is not currently possible, but we could add this I guess 👍 |
You want to:
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.The text was updated successfully, but these errors were encountered: