title: Detect client disconnect
body: <p>I have a very simple socket.io connection.  My server does the following:</p>
  
  <pre><code>io.sockets.on("connection", function (socket) {
      console.log("CONNECTED ON SERVER");                                 
      socket.emit("connected", "CONNECTED!");                                     
  });
  </code></pre>
  
  <p>Indeed, the client gets the "CONNECTED!" message.</p>
  
  <p>I need to be able to detect when a client disconnects and take some action.  I've tried:</p>
  
  <pre><code>io.sockets.on("disconnect", function (socket) {                                 
      console.log("DISCONNECTED!");                                 
  });
  </code></pre>
  
  <p>...but this does not log "DISCONNECTED!" on the server.  However, <code>CONNECTED ON SERVER</code> from above <em>does</em> get logged.</p>
  
  <p>I know that the socket can detect the disconnect because once I close the browser window, the server logs out:</p>
  
  <blockquote>
    <p>info  - transport end (socket end)<br>
      debug - set close timeout for client <br>
      debug - cleared close timeout for client <br>
      debug - cleared heartbeat interval for client <br>
      debug - discarding transport</p>
  </blockquote>
  
  <p>This means that <code>socket.io</code> <em>is</em> able to detect that the client connection closed.  How can <em>I</em> detect that it was closed?</p>