The Missing (
SO_KEEPALIVE socket option setters and getters for Node using
Tested on 🐧
linux & 🍏
arm64), should work on 😈
freebsd and others. Does not work on 🐄
win32 (pull requests welcome).
npm install --save net-keepalive
You can find the full API Reference Document (JSDoc) published on our github pages.
The project includes TypeScript definitions file (
index.d.ts) which gives an overview of the API exposed.
Documentation gets generated from JSDoc comments, feel free to improve them by sending pull requests.
const Net =NetKeepAlive =// or// Create a TCP Serverconst srv = Net// Start on some portsrv// Connect to that serverconst s = Net
iptables add rule to drop all
tcp packets on
INPUT chain to port
iptables -I INPUT -m tcp -p tcp --dport 1337 -j DROP
If you were monitoring packets on
tcp.srcport == 1337 || tcp.dstport == 1337 filter in
wireshark. You will see the following output:
Note: For these methods to work you must enable
SO_KEEPALIVE and set the
TCP_KEEPIDLE options for socket using
Net.Socket-s built in method
socket.setKeepAlive([enable][, initialDelay]) !
TCP_KEEPIDLE (since Linux 2.4) The time (in seconds) the connection needs to remain idle before TCP starts sending keepalive probes, if the socket option SO_KEEPALIVE has been set on this socket. This option should not be used in code intended to be portable.
const NetKeepAlive = require('custom-net-keepalive')// orimport * as NetKeepAlive from 'custom-net-keepalive'// .....const enable = true // enable SO_KEEPALIVEconst initialDuration = 1000 // start probing after 1 second of inactivitysocket.setKeepAlive(enable, initialDuration) // sets SO_KEEPALIVE and TCP_KEEPIDLEconst probeInterval = 1000 // after initialDuration send probes every 1 secondNetKeepAlive.setKeepAliveInterval(socket, probeInterval) //sets TCP_KEEPINTVLconst maxProbesBeforeFail = 10 // after 10 failed probes connection will be droppedNetKeepAlive.setKeepAliveProbes(socket, maxProbesBeforeFail) // sets TCP_KEEPCNT// ....
Code of Conduct