You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.
In order to understand what TCP keepalive (which we will just call keepalive) does, you need do nothing more than read the name: keep TCP alive. This means that you will be able to check your connected socket (also known as TCP sockets), and determine whether the connection is still up and running or if it has broken.
So the following code SHOULD throw something when the internet connection is broken:
It works as expected - though maybe not as you expect it to. :-)
Quoting man 7 tcp:
tcp_keepalive_time (integer; default: 7200; since Linux 2.2)
The number of seconds a connection needs to be idle before TCP begins sending out keep-alive probes. Keep-alives are only sent when the SO_KEEPALIVE socket option is enabled. The default value is 7200 seconds (2 hours). An idle connection is terminated after approximately an additional 11 minutes (9 probes an interval of 75 seconds apart) when keep-alive is enabled.
That is, TCP keep-alive is basically a timer that, once it expires, tells you the connection is dead.
Node.js could perhaps make keep-alive a little more configurable. Linux supports TCP_KEEPCNT, TCP_KEEPIDLE and TCP_KEEPINTVL, the BSDs have TCP_KEEPALIVE, Solaris has TCP_KEEPALIVE, TCP_KEEPALIVE_THRESHOLD and TCP_KEEPALIVE_ABORT_THRESHOLD, Windows has... well, who knows?
Still, that won't let you magically detect a severed connection at the time of the disconnect itself because that's simply not how TCP works.
Quoted from TCP keepalive HowTo:
So the following code SHOULD throw something when the internet connection is broken:
Tested on MacOS/Debian, with NodeJS v0.10.17
The text was updated successfully, but these errors were encountered: