title: Node.js websocket-server crashes when client closes tab
body: <p>I got a little example-project running with a node.js Server which is used to broadcast to clients. </p>
  
  <p>The Server handling the sockets (from <a href="https://www.liferay.com/de/web/james.falkner/blog/-/blogs/liferay-websockets-and-node-good-times-" rel="nofollow">this guy</a> ):</p>
  
  <pre><code>var ws = require('websocket.io');
  var url = require("url");
  var http = require('http').createServer(onRequest).listen(3000);
  var clients = [];
  var server = ws.attach(http, { path: '/mywebsocket' });
  
  server.on('connection', function (socket) {
      clients.push(socket);
      console.log('new client');
      socket.on('message', function (msg) {
          console.log("msg recv: " + JSON.stringify(msg));
      });
      socket.on('close', function () {
          console.log('client leaving ');
          clients.splice(clients.indexOf(this), 1);
      });
  });
  
  function onRequest(request, response) {
      var urlobj = url.parse(request.url, true);
      if (urlobj.query.activity) {
          var actObj = JSON.parse(urlobj.query.activity);
          if (actObj) {
              clients.forEach(function(client) {
                  var msg = JSON.stringify(actObj);
                  client.send(msg);
                  console.log("NODE sending " + msg);
              });
          }
      }
      response.end();
  }
  </code></pre>
  
  <p>The small sample-page:</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
      var node_ws = new WebSocket("ws://" + "localhost" + ":" + "3000" + "/mywebsocket");
  
      node_ws.onmessage = function (evt) {
          var msg = JSON.parse(evt.data);
          var action = msg.action;
  
          var para = document.createElement("p");
          var node = document.createTextNode(action);
          para.appendChild(node);
  
          var element = document.getElementById("div1");
          element.appendChild(para);
      };
  
  &lt;/script&gt;
  &lt;div id="div1"&gt;Test&lt;/div&gt;
  </code></pre>
  
  <p>The Setup works fine, pushing Data to the Client and showing it on the website. When the client leaves by clicking on another link the Server just notes that a client left. When the client leaves by closing the Tab in Browser (in this case Chrome) the Server running on Node.js crashes with the following Error message:</p>
  
  <pre><code>events.js:141
  throw er; // Unhandled 'error' event
  ^
  Error: read ECONNRESET
  at exports.errnoException (util.js:860:11)
  at TCP.onread (net.js:544:26)
  </code></pre>
  
  <p>I tried catching the event of closing with onbeforeunload and closing the clients Websocket, but without success. The </p>
  
  <p>I can't imagine I am the only one having this issue, but I didn't find this exact behavior anywhere else.</p>
  
  <p>Any help is appreciated!</p>