how to implement TCP keepalive?

625 views
Skip to first unread message

Julian

unread,
Mar 14, 2010, 11:12:14 PM3/14/10
to nodejs
Quick question for someone knowledgable...maybe I'm totally missing
this in the documentation...

How do you force node.js to send an ACK packet with no data in it in
order to keep a TCP connection alive? I need to use a TCP keepalive
connection, which is traditionally done by sending an empty ACK to the
peer and forcing it to respond with a similar ACK.

connection.write("", "binary") doesn't send anything at all when an
empty string is passed to it.

Cheers--
Julian

Ryan Dahl

unread,
Mar 14, 2010, 11:21:45 PM3/14/10
to nod...@googlegroups.com

Do you mean SO_KEEPALIVE? I don't have a binding for that yet.

Julian

unread,
Mar 14, 2010, 11:34:06 PM3/14/10
to nodejs
Yes, exactly. If you're busy, I can dig into the code and put a patch
together to add it. I'm guessing adding another method to the
connection object would be the preferred method?
connection.setKeepAlive() ??

Ryan Dahl

unread,
Mar 14, 2010, 11:38:22 PM3/14/10
to nod...@googlegroups.com

Yeah. Follow the example of setNoDelay, it should be straight forward.

Julian

unread,
Mar 15, 2010, 11:02:01 AM3/15/10
to nodejs
Ryan,
I've hacked a bit on it this morning and have it working. There's
one possible issue though: the node.js TCP timeout doesn't seem to
recognize received keepalive packets (it only resets the timer when
data is received). Ideally I think it should probably recognize
those, although I guess you could conceivably turn off the node.js
timeout, and just setup the keepalive parameters to close down the
connection with no activity. I'm definitely not an expert in
sockets...is there even a way for the application layer to get
notified of a received keepalive packet? I've added test/simple/test-
tcp-keepalive.js to demonstrate.

Here's the public repo for you:
git://github.com/thepurlieu/node.git

Let me know if you have any questions or requests you want me to
implement.

Cheers,
Julian

On Mar 14, 11:38 pm, Ryan Dahl <coldredle...@gmail.com> wrote:

Julian

unread,
Apr 2, 2010, 11:42:08 AM4/2/10
to nodejs
Ryan,
Just curious...are you still planning on pulling in my changes into
the main tree? TCP keepalive is critical for our applications at our
company, and we'd obviously prefer not to have to maintain our own
branch forever. As always, let me know if you need any changes made
in order to merge it.

Cheers--
Julian

Reply all
Reply to author
Forward
0 new messages