question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Disconnect event is not fired if socket disconnects before middleware is finished

See original GitHub issue

You 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:closed
  • Created 6 years ago
  • Reactions:7
  • Comments:12 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
TroyKomodocommented, Aug 16, 2020

how is this still a thing?

1reaction
RoDmitrycommented, Jul 25, 2019

@TobiasWehrum thank you for idea. I tried to use ping function:

socket.conn.on('packet', packet => {
  if (packet.type === 'ping' && socket.id in clients) {
    clients[socket.id].seen = Math.trunc(Date.now() / 1000);
  }
});

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:

setInterval(() => {
  const socket_ids = Object.keys(clients);
  socket_ids.forEach(socket_id => {
    if (!(socket_id in io.sockets.connected)) {
      delete clients[socket_id];
    }
  });
}, 900000);
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found