Disconnect event is not fired if socket disconnects before middleware is finished
See original GitHub issueYou want to:
- report a bug
- request a feature
It’s possible this is intended behavior, in which case it would be nice if the documentation explained this clearly.
Current behaviour
If a disconnect
listener is bound on a socket from middleware, the callback is not fired if the socket disconnects before the middleware completes.
My use case for this is along the lines of:
io.use((socket, next) => {
const foo = allocSomeResource(socket);
socket.once('disconnect', () => free(foo));
authorize(socket, next);
})
Where authorize
is a call that completes asynchronously
Steps to reproduce (if the current behaviour is a bug)
server;js
:
const express = require('express');
const app = express();
const server = require('http').createServer(app);
const io = require('socket.io')(server);
const port = process.env.PORT || 3000;
app.use(express.static(__dirname + '/public'));
io.on('connect', onConnect);
io.use(function (socket, next) {
console.log('waiting for ' + socket.id);
setTimeout(next, 1000);
});
server.listen(port, () => console.log('server listening on port ' + port));
function onConnect(socket){
console.log('connect ' + socket.id);
socket.on('disconnect', () => console.log('disconnect ' + socket.id));
}
client.js
const socket = require('socket.io-client')('http://localhost:3000');
socket.on('connect', onConnect);
function onConnect(){
console.log('connect ' + socket.id);
}
setTimeout(() => socket.disconnect(), 500);
Expected behaviour
disconnect
event is fired when the socket is closed OR the documentation clearly states whether/which events are supported during the middleware phase.
Setup
- OS: Linux
- browser: N/A, reproduced with node.js client
- socket.io version:
socket.io@2.0.4
Other information (e.g. stacktraces, related issues, suggestions how to fix)
Issue Analytics
- State:
- Created 6 years ago
- Reactions:7
- Comments:12 (2 by maintainers)
Top Results From Across the Web
socket.io: disconnect event isn't fired - node.js - Stack Overflow
This way you're detecting when a specific socket (specifically the socket ... because the disconnect event is only fired once per socket.
Read more >The Socket instance (client-side)
This event is fired upon disconnection. ... In the first two cases (explicit disconnection), the client will not try to reconnect and you...
Read more >Create a Secure Chat Application with Socket.IO and React
The constructor of the Connection class sets up callbacks on events coming from the socket. The disconnect and connection_error are predefined ...
Read more >AGServerSocket - SocketCluster
Happens when the client disconnects from the server before the SocketCluster handshake has completed (I.e. while socket.state was 'connecting'). Note that the ' ......
Read more >Connecting and disconnecting (Concepts) - Prisma
PrismaClient connects and disconnects from your data source using the following two methods: ... In most cases, you do not need to explicitly...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
how is this still a thing?
@TobiasWehrum thank you for idea. I tried to use ping function:
But I noticed that’s actually overkill, because socket.io disconnects all of the not pingable clients itself. And others left are not found in connected, so my workaround is: