user3506697 user3506697 - 4 months ago 20x
Node.js Question

How to immediately know if connection is lost with Socket.IO?

I'm working on a project that includes BeagleBoneBlack and web server written in NodeJs. The idea is that when Beaglebone detects something from enviroment sends it to web server, and if connection is lost for some reason BBB stores the log in it's own database.

So I use SocketIO, and I emit when BBB detects something. I use flag variable isConnected that I put on false on "disconnect" event, and if isConnected is false I'm not emiting to server, just writing in database.

Problem is that when computer, where server is running, goes to sleep (simulating lost connection), SocketIo needs sometimes more than a minute to detect that connection is lost, and emit disconnect event. Is there any way to get this info faster, because the program tries to send readings to server and can't, but it's not written in database.


The client could perhaps emit myping every 5 seconds (or more often) and receive mypong from server confirming that the server is working.

...or the server can send the confirmation of receiving data and the client tries to resend data until it receives the confirmation.