Javascript – detecting if server is down


Is there something that I can do on the client side to detect that the websocket is not available? Something along the lines of:

  • server starts as per usual
  • clients connect
  • messages are sent back and forth between server and client(s)
  • server shuts down (no longer available)
  • warn the connected clients that the server is not available

I tried to add the 'error' and 'connect_failed' options on the client side but without any luck, those didn't trigger at all. Any ideas from anyone how I can achieve this?

Best Solution

The disconnect event is what you want to listen on.

var socket = io.connect();

socket.on('connect', function () {
  alert('Socket is connected.');

socket.on('disconnect', function () {
  alert('Socket is disconnected.');