-
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
Not able to get the list of rooms the client is currently in on disconnect event #1814
Comments
That answer is correct, by the time the 'disconnect' event is emitted the socket has left the rooms. You can either override the onclose handler as suggested or you can store the rooms in whatever mechanism you use for your session store. |
+1, I would like to inform other users in the same room that someone has disconnected. So my code would look like :
|
+1 I would also find this useful, however we would also need to make sure this happened before the empty room cleanup happens. Otherwise we run into a case where the room does not exist. |
I stumbled across the same situation. As suggested one dirty workaround is by overriding the the onclose handler. But it would also work if socket.io would just send something like a "isDisconnecting" event right before the actually cleanup happens. This could be right before the call to this.leaveAll() in the onclose handler happens: Socket.prototype.onclose = function(reason){
...
this.emit('disconnecting', reason); //<--insert the new event here
this.leaveAll();
...
this.emit('disconnect', reason);
}); (the exports.events array has to be extended by the new eventname to get this working) Then you can use the new event like this: socket.on('disconnecting', function(reason){
var id = socket.id;
socket.rooms.forEach(function(room){
// For each room the user is in, excluding his own room
if(room != id){
console.log('Room ' + room + ': a user is disconnecting and leaving this room');
}
});
}); |
+1, this has caused me no end of headaches in my code. It would simplify so much. |
Just experienced this. Would be nice to have a disconnecting event or something along those lines. |
+1, this is very frustrating |
+1 |
2 similar comments
+1 |
+1 |
I have the same problem to solve it. You can develop an in-memory object If you are using some DB o NoSQL you can save it for distributed Additonally, if rooms list is temporary you can clean it whit controlled El vie., 24 de julio de 2015 13:44, Max notifications@github.com escribió:
|
+1 |
plus elebenty, however did find bodyflex's implementation useful |
+1 I tried this solution: io.on('connection', function(socket) {
console.log('CONNECT');
socket.onclose = function(reason) {
var roomId = socket.rooms[1];
console.log(roomId);
Object.getPrototypeOf(this).onclose.call(this, reason);
}
// ...
} And It's works, but I have a feeling that this is not the right way |
+1 |
Hi! I suggested here (#2266) adding 'roomJoined' and 'roomLeft' events, would such a solution suit your needs? server.on('roomJoined', function (id, room) {
console.log('Socket %s has joined room %s', id, room);
});
server.on('roomLeft', function (id, room) {
console.log('Socket %s has left room %s', id, room);
}); |
Your suggestion goes a little into a different direction, but will work too. |
Well, on second thought, your solution seems cleaner to me. Please note that you may have to clone the array client.on('disconnecting', function(){
console.log(client.rooms);
var clonedRooms = client.rooms.slice();
setTimeout(function() {
console.log(client.rooms); // empty array
console.log(clonedRooms);
}, 100);
}); |
It's a must have feature to have pre-disconnect event. Handling cleanup is much harder without this. |
I am currently overriding the |
I also added a disconnecting event that fires before disconnect and came to submit a PR to find that someone had already submitted a PR with nearly identical code :). +1 for PR #2332 ! |
Closed by #2332 👼 |
You don't need to add events, you need to store all the connected sockets into an associative array with keys being the socked IDs. Then, on 'disconnect', you retrieve the room for that socket ID.
|
Hello everyone, how I was able to solve this was like so: socket.on(disconnecting,()=>{ Thanks, hope it helps someone |
@darrachequesne I'm trying to use an async function on |
@giovanni-bertoncelli in that case, you will need to create a copy of the io.on("connection", (socket) => {
socket.on("disconnecting", async (reason) => {
const rooms = new Set(socket.rooms);
await someLongRunningOperation();
// socket.rooms will be empty there
console.log(rooms);
});
}); Added here: https://socket.io/docs/v4/server-api/#event-disconnecting |
Ok thank you very much!! |
I am trying to find the list of rooms a client is currently in on disconnect event (closes the browsers / reloading the page / internet connection was dropped) and based on my question on SO it looks like it is not possible.
I find it hard to believe that this is the case (I think this is frequently needed functionality). Can any socket.io guru tell whether this is true or even better how can I achieve this?
The text was updated successfully, but these errors were encountered: