title: NodeJs/WS: How to throw an error server side handled on client side?
body: <p>I'm trying to throw and error server side when my websocket has more than 2 connections. I have this nice client-side <code>onerror</code> method but I'm unable to reach that part of my code. I'm using nodeJS and the package <code>ws</code> which has the smallest doc on error handling.</p>
  
  <p>server.js</p>
  
  <pre><code>theWebSocketServer.on('connection', function connection(websocket){
      if (theWebSocketServer.clients.length &gt;2) {
          // I want to throw the error here and pass it to onerror
          console.log('No access allowed', theWebSocketServer.clients.length)
      } else {
          console.log('happy connection', theWebSocketServer.clients.length)
      }
  })
  </code></pre>
  
  <p>client.js</p>
  
  <pre><code>wsConnection.onerror = function(eventInfo) {
     alert("There was a connection error!");
     console.log("Socket error!", eventInfo);
  }
  </code></pre>
  
  <p>How can I send an error to be handled on the client side JS? </p>