Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 461
kojow7 Avatar asked Oct 16 '25 15:10

kojow7


2 Answers

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).

like image 197
jfriend00 Avatar answered Oct 18 '25 08:10

jfriend00


Yes. And you can set options.

var myApp = require('express')();
var http = require('http').Server(myApp);

var io = require('socket.io')(http, {'pingTimeout': 7000, 'pingInterval': 3000});
like image 20
Denis Lisitskiy Avatar answered Oct 18 '25 08:10

Denis Lisitskiy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!