socket.io icon indicating copy to clipboard operation
socket.io copied to clipboard

Lagged client disconnect on server terminate

Open aarong opened this issue 15 years ago • 8 comments

The client takes around 15 seconds to emit the disconnect event when I terminate my Node server. The client noticed such disconnects instantly before I upgraded to 0.7. I found the problem to arise in both Chrome (web sockets) and FF3 (XHR), and did not try any other browsers. Note that when the disconnect() method is used explicitly on the server, then the client emits a disconnect immediately.

To replicate, copy the following files into a folder and run "sudo node server.js"

----- server.js -----

var io = require('socket.io'),
    http = require('http'),
    fs = require('fs');

var server = http.createServer(function (req, res) {
        res.writeHead(200);
        res.write(fs.readFileSync('index.html'));
        res.end();
    })
  , io = io.listen(server);

server.listen(80);

io.sockets.on('connection', function (socket) {

    // socket.disconnect(); // Uncomment and the client will see the disconnect immediately
    process.exit()

});

----- index.html -----

<script src="/socket.io/socket.io.js"></script>
<script>

var socket = io.connect('/');

socket.on('connect', function () {
    console.log('Connected');
    socket.send('Hello from the client');
});

socket.on('disconnect', function () {
    console.log('Disconnected');
});

</script>

aarong avatar Jul 24 '11 18:07 aarong

+1

saschagehlich avatar Jul 25 '11 18:07 saschagehlich

Actually you can fix this by simply setting the heartbeat timeout, heartbeat interval and close timeout settings.

I set heartbeat timeout to 2, heartbeat interval to 1 and close timeout to 2 as well. Setting the intervals and timeouts to the same value could cause random disconnect events due to network laziness.

saschagehlich avatar Jul 25 '11 19:07 saschagehlich

This is a good work-around, but shouldn't the client instantly emit hard disconnects? If I kill the server then the socket.io client should see the web socket close instantly without waiting for the next heartbeat.

aarong avatar Jul 27 '11 13:07 aarong

I'd like to offer my heartfelt +1 for this issue.

To my understanding, the client in version 0.6.x detects instances of losing connection to the server by using transport-specific logic, and it does indeed fire the disconnect event as soon as that happens. For 0.7.x, those bits of code have been deprecated, and to replace them they introduced a sort of a one-size-fits-all system to detect and fire client-side disconnection events based on a user-set inactivity timeout, dubbed the "close timeout". These changes took place to accommodate the new "reopening" feature, which makes possible the renewal of the closed connection within a certain time-frame (the close timeout), maintaining the same session ID.

It's possible that my limited ability hasn't allowed me a full understanding of the situation, but I think that's the gist of it.

Anyway, I sincerely appreciate all the effort put in Socket.IO and new features, but I think this is bullshit. The old version's behavior of immediate client-side disconnection-detection seems to have worked beautifully for a lot of people, and we're now confused as to what to do in light of these new changes. I'd really love to be able to opt-out of this new feature and use the old way instead. I mean, the code is already there, isn't it?

Thank you.

5HT2A avatar Aug 27 '11 18:08 5HT2A

So, I completely agree. Is the still-in-development "reopen" feature supposed to address this?

Regardless, will socket.io be able to deal with using "reopen" to connect to a new server instance? (i.e. the node server has restarted).

I have a suggestion: Either change the meaning of the closeTimeout, or add a new timeout option called 'reopenTimeout' that starts as soon a disconnection is noticed. This would enable people who prefer the old behavior (begin reconnection immediately) to set this timeout to 0.

danielbeardsley avatar Sep 24 '11 17:09 danielbeardsley

I commented in another thread:

It's somewhat frustrating now because with v7 we have two levels of connection (socket.io and the underlying transport)We have auto-reconnection for the socket.io level, but not for the underlying transport. I think the old methodology was simpler (only one connection layer) because now we are forced to re-implement reconnection on each layer.

Waiting for the closeTimeout with no feedback from socket.IO is NOT an option, that leaves the client with no way to tell if it's actually connected or not.

Seems like we have a three options:

  • Have another event like connectionSuspended that fires the instant the underlying transport is disconnected and then fire connectionResumed when reconnection happens before the closeTimeout.
  • Ditch the idea of separate connection layer state and make a transport disconnection initiate a real socket.io disconnection just like previous versions.
  • Keep the closeTimeout, and flesh out the connection state for the underlying transports, issuing events like "transportDisconnected" and "transportConnected" and handle auto reconnection in a similar (though simpler) manner as the socket.io layer.

Note: previous versions of Socket.IO were so stable that my socket.IO connection was my defacto "Am I connected to the internet" test.

danielbeardsley avatar Sep 26 '11 02:09 danielbeardsley

I've fixed this issue, please see pull-request: https://github.com/LearnBoost/socket.io-client/pull/315

danielbeardsley avatar Oct 06 '11 07:10 danielbeardsley

Shouldn't be an issue in the latest release. Don't hesitate to reopen if this comes up again.

rase- avatar Jul 22 '14 23:07 rase-