Skip to content
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

onclose not called if a client disconnects due to network failure #686

Closed
pavan4 opened this issue Feb 19, 2016 · 15 comments
Closed

onclose not called if a client disconnects due to network failure #686

pavan4 opened this issue Feb 19, 2016 · 15 comments

Comments

@pavan4
Copy link

pavan4 commented Feb 19, 2016

I am listening to the close event from a client on a node server using websocket.on('close', function close() { } );

However, the callback is not being fired if a client gets disconnected due to network failure.

Is there any way to detect dead connections on the server side ?

@pavan4
Copy link
Author

pavan4 commented Feb 19, 2016

https://github.com/FlorianBELLAZOUZ/ws/tree/KeepAlive This provides an implementation for the above issue

@urosjarc
Copy link

@pavan4 I have the same issue, your implementation is quite good... I suggest developers to merge in master... 👍

@pavan4
Copy link
Author

pavan4 commented Feb 22, 2016

@urosjarc The implementation I mentioned above does this out of the box for you with pings but it doesnt work on ubuntu 14.0.4 and the timeout on tcp defaults to 30s always. Pings to the client result in error and are crashing the server, I am not able to rely on the pings.

@urosjarc
Copy link

Then what are your suggestions? I know only pings checking if connection is alive...

@pavan4
Copy link
Author

pavan4 commented Feb 22, 2016

I am planning to use https://github.com/theturtle32/WebSocket-Node This package has similar performance as ws and provides a onclose notification when the client disconnects

@urosjarc
Copy link

Did you check onclose method for network drop, does it really emit?

@r-pr
Copy link

r-pr commented Oct 17, 2016

I've got the same problem with detecting connections experiencing network errors. I've tried setting keepAlive, as pointed here, but close event still fired only 10 s after network got down. Finally, I figured out how to use Websocket ping-pong mechanism:

var WebSocketServer = require('ws').Server,
    wss = new WebSocketServer({ port: 4444 });

var PONG_TIMEOUT = 1500; //client has 1.5s to respond with pong

wss.on('connection', function connection(ws) {
    var lastPongReceived = Date.now();
    ws._socket.on('data', function(data) {
        if (data[0] == 0x8a) { //opcode of pong response
            lastPongReceived = Date.now();
        }
    });

    var x = setInterval(function() {
        if (Date.now() - lastPongReceived > PONG_TIMEOUT) {
            console.log('pong timeout elapsed');
            //...code to run if connection unavailable
            clearInterval(x);
            ws._socket.destroy(); //ws.close() won't raise 'close' event, because close message 
                                   //cannot be delivered to client      
        }
        if (ws.readyState === 1) {
            ws.ping('.', false);
        } 
    }, 1000);

    ws.on('close', function() {
        console.log('connection closed');
    })
});

@john-doherty
Copy link

@r-pr how has the above ping/pong code worked out for you? Have you used it in production and if so how many connections? Thanks!

@urosjarc
Copy link

urosjarc commented Jan 24, 2017

This is my implementation:
https://github.com/urosjarc/websocketRest.js
It's used in production for 1 year +2000 mobile devices connected which are switching connections (wifi, 3G) all the time. We were having many problems with duplicats but now this lib is running 1 year without error.
This is the company that use this lib: http://facilityforhotels.com/

@urosjarc
Copy link

I will add additional docs if there will be an interest in this lib.

@r-pr
Copy link

r-pr commented Jan 25, 2017

@john-doherty It seems that the mechanism does the job, but I haven't used it in production.

@john-doherty
Copy link

@r-pr I'm going to use a slightly modified version of your concept. I'll let you know how it goes.

@r-pr
Copy link

r-pr commented Jan 25, 2017

@john-doherty OK

@zs9024
Copy link

zs9024 commented Apr 14, 2017

has the issue been solved in the latest version?

@lpinca
Copy link
Member

lpinca commented May 12, 2017

A possible way to handle this issue is illustrated here: https://github.com/websockets/ws#how-to-detect-and-close-broken-connections.
Closing, feel free to continue discussing on the closed thread.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants