title: Node.js: socket.io close client connection
body: <p>How can I close the socket connection on the client side?</p>
  
  <p>I am using:  </p>
  
  <ul>
  <li>socket.io 0.9</li>
  <li>node.js 0.10.15</li>
  <li>express 3.3.4</li>
  </ul>
  
  <p>i.e.:
  call <code>localhost/test</code><br>
  -- server side</p>
  
  <pre><code>var test = io
  .of('/test')
  .on('connection', function (socket) {
  
    console.log('open socket: ' + socket);
  
    socket.on('disconnect', function () {
      console.log('disconnected event');
      //socket.manager.onClientDisconnect(socket.id); --&gt; endless loop with this disconnect event on server side
      //socket.disconnect(); --&gt; same here
    });
  });
  </code></pre>
  
  <p>-- client side</p>
  
  <pre><code>var socket = io.connect('http://localhost:3000/test');
  socket.on('disconnect', function () {
     console.log('disconnect client event....');
  });
  
  socket.emit('getInitData', function (data) {
    .. do something with data
  });
  </code></pre>
  
  <p>If I load the test-page I need some values from the server (getInitData).<br>
  On the first page visit I get the data once, on a reload or second visit I get it twice and so on.  </p>
  
  <p>The connection on the server side is beeing closed automatically on page reload and if you leave the page.<br>
  But on the client side the connection is still open.<br>
  How can I close the connection on the client side or check if there is already a open connection?</p>
  
  <p><strong>UPDATE</strong><br>
  I tried now the following: (client side)</p>
  
  <pre><code>window.onbeforeunload = function(e) {
    socket.disconnect();
  };
  </code></pre>
  
  <p>This triggers on the client side the disconnect event, but I still get the twice or tripple response.</p>