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

How to check client is connected in web socket? #353

Closed
waleedbutt opened this issue Aug 9, 2014 · 23 comments
Closed

How to check client is connected in web socket? #353

waleedbutt opened this issue Aug 9, 2014 · 23 comments

Comments

@waleedbutt
Copy link

My question is that I am using web socket for my iphone native app so if user is disconnected the internet then web socket cannot get the state of client that it is close or disconnect. my code is here
var webSocketServer = new (require('ws')).Server({port: (process.env.PORT || 5000)}),
webSockets = {} // userID: webSocket
//Delete userID from connected list of userID
webSocket.on('close', function ()
{
delete webSockets[userID]
util.log('User_id: [' + userID+'] deleted from Connected users list: ['+ Object.getOwnPropertyNames(webSockets)+' ]');

})

but if i use that code it did not work.

// if client is disconnect with internet
webSocket.on('disconnect',function()
{
console.log('hello i am disconnected');
});

@zwz
Copy link

zwz commented Aug 22, 2014

I have the same problem.
I debuged my nodejs program, and when the client disconnected,
the server did know anything, and kept the connection for a long time
even after the client reconnected.

The weird thing is that even the device is disconnected, when the server tries to send some message to the device, there is no error and the server still has no idea about the dead connection.

This is quite annoying, because it could make my data inconsistent.

BTW, I am using https://github.com/mkuklis/phonegap-websocket on my android 4.0.4 device.

@adis-io
Copy link

adis-io commented Aug 23, 2014

👍

@waleedbutt
Copy link
Author

@zwz But I want web socket for ios to remove the dead connection?

@zwz
Copy link

zwz commented Aug 26, 2014

@waleedbutt It seems the server will remove them after some time.

@humingchun
Copy link

I had the same issue and after some Googling, here is result:

WebSocket is based on TCP and TCP uses FIN packet to close the connection. In the case of sudden loss of Internet connection, both the WebSocket server and the phone are unware of the already dead TCP connection because no FIN packet was sent.

To address the issue, TCP has a mechanism called keepalive.

What I did to solve the issue is adjusting the TCP keepalive settings in Linux kernel, and invoke ws._socket.setKeepAlive(true).

I am going to propose a new public method setKeepAlive to ws so it clearly shows a way for ws to use the underlying TCP mechanism to maintain connection or detect dead connection.

@waleedbutt
Copy link
Author

Hi humingchun
Thanks for update. Can you please tell me that after set ws._socket.setKeepAlive(true). How socket disconnect those client or delete from connected user list ?

@humingchun
Copy link

Hi waleedbutt, for the detailed introduction for TCP keepalive machanism, you could refer to the link I posted before.

And after the keepalive detects the failure of the connection, TCP layer will raise a close event which is forwarded to ws. So you could just listen to ws's close event and do any necessary clean ups.

For example, you want to detect a dead TCP connection within 10 seconds, you can set following kenel params:

net.ipv4.tcp_keepalive_time=6
net.ipv4.tcp_keepalive_intvl=2
net.ipv4.tcp_keepalive_probes=2

It will add a little more extra network traffic, but you can choose to enable keep alive to specific TCP connection by invoking ws._socket.setKeepAlive(true)

@binarykitchen
Copy link

this is interesting - but let me clarify something: what when i am on the road with the mobile, having an open websocket connection to the server for app X but loose connection for few seconds during a cell handover? will the above trick resume the same connection or create a new one?

@Spentas
Copy link

Spentas commented Nov 21, 2015

binarykitchen,it depends on your client side implementation.As i tested on android, immediately after re-connection it will close the current session and generate a new one.

@binarykitchen
Copy link

why can't we re-use the session when re-connecting? @Spentas

@Spentas
Copy link

Spentas commented Nov 22, 2015

simply cause your ip address may be changed upon reconnection and the old session is bound to an invalid ip address

@binarykitchen
Copy link

hmmm, are websocket sessions really bound to an ip address instead of a token??

@vvsevolodovich
Copy link

binarykitchen, websocket is basically a tcp/ip connection, there is no reconnection option. What we do in our android app, we use telephone manager to subscribe on cell change event. If it triggers, we just do the reconnect manually

@binarykitchen
Copy link

sure i understand that. reason i am asking all that is because my www.videomail.io app is having issues on androids when you are moving from one cell to another. when reconnected, new streams are instantiated instead of the existing one which makes it very difficult to send data to the same destination on the server side.

@theavijitsarkar
Copy link

How to implent the fix @humingchun . Where do i set ws._socket.setKeepAlive?

@AbrarJahin
Copy link

Hi, I am using this library in Node.js - https://www.npmjs.com/package/express-ws
To implement websocket.

But I am not finding any way of running this code-

ws._socket.setKeepAlive(true)

Can anyone please help?

@slava-osipov-qs
Copy link

slava-osipov-qs commented Aug 9, 2016

Hi,

ws._socket.setKeepAlive

You can found answer here for WS level implementation of disconnect detection.

@lpinca
Copy link
Member

lpinca commented May 12, 2017

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

@lpinca lpinca closed this as completed May 12, 2017
@Bomper
Copy link

Bomper commented Jan 11, 2018

@lpinca: that's only for verifying as a server, that the client is still connected. How about reconnecting, as a client, when the connection no longer sends data for a while?

@lpinca
Copy link
Member

lpinca commented Jan 11, 2018

Add a timer on the client that terminates the connection when it expires, reset the timer every time a ping (or any other data) is received from the server.

@Bomper
Copy link

Bomper commented Jan 12, 2018

@lpinca: that's what I did in this sample, but it's not that straightforward to reconnect. Not even reconnecting-websocket manages to.

@kashishkhullar
Copy link

Is there any close event that the websockets emit which we can listen to whenever a connection breaks. eg:
var sockets = [ ] ; // here i save all the socket
then I do this
sockets.forEach( socket => socket.on('close', console.log("Connection closed"));

or can I add the same logic whenever a new connection opens?

@kenethcinco
Copy link

setTimeout(function() {
console.log(socket.connected);
},500);

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