Search code examples
node.jssocket.iokeep-alivelinode

Does socket.io handle keepalives automatically?


I am using node.js and socket.io on a Linode server. I was told that I should use KeepAlives with my app in order to make use of a Linode NodeBalancer. I was also told that I had to set the NodeBalancer protocol to use TCP rather than (HTTP or HTTPS which both disable KeepAlives).

My question is, are KeepAlives already implemented in socket.io by default? There seems to be something keeping the connection alive so I am thinking that this is already handled by socket.io. However, I cannot seem to find any info on this topic. If KeepAlives are not implemented by default in socket.io what problems will I face using the NodeBalancer? If I should be implementing KeepAlives what steps do I need to take to implement them in my app?

As an additional question, if KeepAlives are implemented in socket.io, what issues will I face if I use the HTTP option rather than TCP?


Solution

  • My question is, are KeepAlives already implemented in socket.io by default?

    Yes, a ping packet (a very small webSocket packet) is sent every so often as a heartbeat to "test" the connection and make sure it is still functional and to keep it alive. A pong packet is returned from the ping request so the sender knows whether the ping got through or not.

    This ping aspect of socket.io is very poorly documented, but by examining the source code of engine.io which socket.io uses, there is both a pingTimeout (how long to wait for a pong response and a pingInterval (how often to send a ping).