title: Handling connection loss with websockets
body: <p>I've recently set-up a local WebSocket server which works fine, however I'm having a few troubles understanding how I should handle a sudden loss of connection which neither the client or server intentionally initiated, i.e: Server loses power, ethernet cables pulled out etc... I need the client's to know whether connection has been lost within ~10seconds.</p>
  
  <p>Client side, connection is simply:</p>
  
  <pre><code>var websocket_conn = new WebSocket('ws://192.168.0.5:3000');
  
  websocket_conn.onopen = function(e) {
      console.log('Connected!');
  };
  
  websocket_conn.onclose = function(e) {
      console.log('Disconnected!');
  };
  </code></pre>
  
  <p>I can manually trigger the connection disconnect which works fine,</p>
  
  <pre><code>websocket_conn.close();
  </code></pre>
  
  <p>But if I simply pulled the ethernet cable out the back of the computer, or disabled the connection, <code>onclose</code> doesn't get called. I've read in another post that it would eventually get called when <a href="https://stackoverflow.com/questions/11809267/how-do-i-know-if-connection-is-alive-with-websockets">TCP detects loss of connectivity</a>, but it's not in the timely manner that I need as the default for Firefox I believe is 10 minutes, and I don't really want to go around hundreds of computers <code>about:config</code> changing this value. The only other suggestion I've read is to use a 'ping/pong' keep-alive polling style method which seems counterintuitive to the idea of websockets.</p>
  
  <p>Is there an easier way to detect this kind of disconnect behaviour? Are the old posts i'm reading still up to date from a technical point, and the best method is still 'ping/pong' style?</p>