how to keep alive a socket in node.js in appfog


I have a long running socket which I need to keep alive even when the system is idle. For that I have tried everything from reduced long polling duration to a server and client ping pong messages but, nothing seems to work as the socket gets disconnected after some duration let's say 1 hour. But the socket connection needs to be alive till I manually close it.

How do I achieve this?

Also the latest version of node.js semms to support socket.setTimeOut(0) by which I can keep the socket open indefinitely but setTimeOut method seems to be non existent for socket in the version I am using as it shows method not defined error. The version of node I am using is 0.8.

For ping pong messages I am using this code

for reduced long poll duration I have used

and for socket.settimeout on connection event

Any help on this will be much appreciated :)

Problem courtesy of: Nav


hmmmm... couldn't find the cause even though I believe it to be the evil work of the gfi web monitor the company is using but did find a solution...

The solution was to connect as soons as the disconnect event happened and again register the info on connect event (which is needed if only u r maintaing a separate list of info for the sockets ...say u r maintaing a list of online users)

socket.on("disconnect", function() {



socket.on("connect", function() {
                //do the registration code within this event
Solution courtesy of: Nav


There is currently no discussion for this recipe.

This recipe can be found in it's original form on Stack Over Flow.