0

I am confused on how can server/client know that the other has been disconnected and have several questions:

  1. How can server detect that client has been disconnected and trigger function in socket.on('disconnect') on serverside. Similar to client, it also has socket.on('disconnect') will trigger on clientside when server closed. I have set the very high pingInterval and very high pingTimeout on server but it seems both server and client can detect the other has been disconnected immediately (by trigger on('disconnect') event).

  2. About ping/pong packet, is that both sides (server and client) will send a 'ping' to the other and wait for the 'pong' response back or the only serverside send 'ping' to client and client send back 'pong' to server when it receives 'ping' packet.

Anyone know it please explain to me. Thanks very much

Quoc Van Tang
  • 1,075
  • 4
  • 15
  • 33
  • 1
    Check this way to create/register the disconnect event, probably you are missing something. Share your code to get a better help. https://stackoverflow.com/questions/9868846/node-js-doesnt-send-socket-on-disconnect-event – F.Igor Sep 19 '18 at 02:18
  • @F.Igor Thanks for your reply, but seems you didn't get my mean. I know how to register the disconnect event but I want to know how can server and client detect the disconnect and trigger that event – Quoc Van Tang Sep 19 '18 at 02:28
  • Are you searching for technical details about TCP connections? See for example http://stefan.buettcher.org/cs/conn_closed.html – F.Igor Sep 19 '18 at 02:40
  • @F.Igor Yes, I've read that article, so it seems the disconnect detection on both server and client side is not related to the ping/pong mechanism, right? – Quoc Van Tang Sep 19 '18 at 05:06

1 Answers1

0

How can server detect that client has been disconnected and trigger function in socket.on('disconnect') on serverside. Similar to client, it also has socket.on('disconnect') will trigger on clientside when server closed. I have set the very high pingInterval and very high pingTimeout on server but it seems both server and client can detect the other has been disconnected immediately (by trigger on('disconnect') event).

A regular disconnect is detected when the TCP connection is closed for whatever reason. The server will see a TCP connection get closed when a web page either closes the socket.io connection manually or when the page is navigated away from and the browser closes all open connections from that web page. So, when the connection is operating normally and is then closed by either client or server, the other end will see the closed TCP connection immediately. This has nothing to do with the ping or pong intervals. Those are used for something else (described below).

Since a TCP socket can still be technically connected (not closed yet), but may actually be inoperable and not working any more, ping and pong are used to determine if a connection that still has such an open TCP socket is no longer working. If that is found to be the case (not getting a timely response from the other side from ping/pong), then the socket is closed and the disconnect event is then generated.

About ping/pong packet, is that both sides (server and client) will send a 'ping' to the other and wait for the 'pong' response back or the only serverside send 'ping' to client and client send back 'pong' to server when it receives 'ping' packet.

I cannot find any definitive documentation on this, but it looks like the server code is expecting to receiving ping messages from the client and it will close the connection if no ping or other data packet is received within a certain timeout period. As soon as the server receives a ping, it immediately sends a pong back to the client. So, presumably the client is sending the ping and looking for a pong response within a certain amount of time. If it doesn't find the pong response within a certain time, then it will close the connection on its end (and may attempt a reconnect, depending upon configuration).

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • Thanks for your explanation, so your mean is when the client close webpage or the server got closed, it will send the disconnect event to the other side while the connection still connected (maybe in inactive status). And the ping/pong will help to check inactive status, or any other connection issue then completely destroy the connection right? – Quoc Van Tang Sep 19 '18 at 06:44
  • @QuocVanTang - No, it doesn't send the disconnect event at the socket.io level. It closes the TCP socket and part of the TCP protocol notifies the other end that the socket has now been closed which the other end will be notified of and that will trigger the socket.io disconnect event on the other end. ping and pong are used to detect a broken, but still technically connected TCP socket. A lack of proper response means that either the other end or the transport must not be working properly. In either case, the remedy is to close the connection and, depending upon configuration, reconnect. – jfriend00 Sep 19 '18 at 06:49
  • Nice, I almost got it. Thank you again @jfriend00 – Quoc Van Tang Sep 19 '18 at 06:57