title: Can't connect to local Node.js secure WebSocketServer
body: <p>For testing a JavaScript / html5 application, I created a local WebSocketServer with node.js and <a href="https://www.npmjs.com/package/ws" rel="noreferrer">ws package</a>. I want to use secure websockets (wss) with SSL/TLS.</p>
  
  <p>Key and certificate were create for testing purposes by OpenSSL locally (self signed certificate).</p>
  
  <p>The client just tries to use the native WebSocket object to connect to the (local) https Server:</p>
  
  <pre><code>var ws = new Websocket('wss://localhost:8080');
  </code></pre>
  
  <p>The Problem is, <strong>no</strong> browser (Firefox, Chrome, Edge) can connect to the server and they all give me <strong>different error messages.</strong></p>
  
  <p>Firefox: </p>
  
  <blockquote>
    <p>Firefox can not connect to the server at wss: // localhost: 8080 /.</p>
  </blockquote>
  
  <p>Chrome: </p>
  
  <blockquote>
    <p>ws_client.js:7 WebSocket connection to 'wss://localhost:8080/' failed:
    Error in connection establishment: net::ERR_CERT_AUTHORITY_INVALID</p>
  </blockquote>
  
  <p>Edge: </p>
  
  <blockquote>
    <p>SCRIPT12017: SCRIPT12017: WebSocket Error: SECURITY_ERR, Cross zone
    connection not allowed</p>
  </blockquote>
  
  <p>I created the certificate and key in OpenSSL (light, newest version) like this:</p>
  
  <pre><code>openssl req -new -x509 -nodes -out server.crt -keyout server.key
  </code></pre>
  
  <p><a href="https://stackoverflow.com/questions/14267010/how-to-create-self-signed-ssl-certificate-for-test-purposes#comment34261983_14267011">(source)</a></p>
  
  <p>I checked almost every question about this (and similar) topics, e.g. <a href="https://stackoverflow.com/questions/23404160/why-does-my-wss-websockets-over-ssl-tls-connection-immediately-disconnect-w">this question</a>, but none of them could provide a solution. </p>
  
  <p>Please do not mark this question as a duplicate, because all similar questions contain slightly different problems!</p>
  
  <p>Server Code:</p>
  
  <pre><code>var fs = require('file-system');
  
  var pkey = fs.readFileSync('server.key', 'utf8');
  var crt = fs.readFileSync('server.crt', 'utf8');
  
  var credentials = { key: pkey, cert: crt };
  var https = require('https');
  
  var httpsServer = https.createServer(credentials);
  httpsServer.listen(8080);
  
  var WebSocketServer = require('ws').Server;
  
  var wss = new WebSocketServer({
      server: httpsServer
  });
  
  wss.on('connection', function connection(ws) {
      ws.on('message', function incoming(message) {
          console.log('received: %s', message);
          ws.send('reply from server : ' + message)
      });
  
  });
  </code></pre>
  
  <p>I tried another code as server, but same errors occur:</p>
  
  <pre><code>const WebSocketServer = require('ws').Server; 
  var fs = require('file-system');
  
  var ws_cfg = {
      ssl: true,
      port: 8080,
      ssl_key: 'server.key',
      ssl_cert: 'server.crt'
  };
  
  var processRequest = function(req, res) {
      console.log('Request received');
  };
  
  var httpServ = require('https');
  
  var app = httpServ.createServer({
      key: fs.readFileSync(ws_cfg.ssl_key, 'utf8', (error) =&gt; {
          console.log('Error reading file');
      }),
      cert: fs.readFileSync(ws_cfg.ssl_cert, 'utf8',  (error) =&gt; {
          console.log('Error reading file');
      })
  }, processRequest).listen(ws_cfg.port, function(){
      console.log('Server running');
  });
  
  
  
  var wss = new WebSocketServer( {server: app, port: 8080, host: 'localhost', domain: 'localhost'} );
  
  
  
  wss.on('connection', function (ws) {
  
      console.log('Connected to a client');
  
      ws.on('message', function (message) {
  
          console.log('MSG received: ' + message);
  
      });
  
  });
  </code></pre>
  
  <p>There's one more thing. Always, if I add a <code>console.log(wss);</code> to the server Code, the output looks something like this:</p>
  
  <pre><code>WebSocketServer {
    domain: null,
    ...some more stuff...
    ...cert key etc....
    host: null,
    path: null,
    port: null } }
  </code></pre>
  
  <p>host, domain and port is set to <code>null</code>. I tried everything to set it to localhost:8080, but nothing worked out. I think this could be the source of all Problems, but can't find a way. If anyone knows an answer to this question, I would highly appreciate it.</p>
  
  <p><em>(Using the insecure 'ws' protocol ('ws://localhost:8080') in order to connect to local node.js http server works, but I want to test the app as realistic as possible and use a secure Connection.)</em></p>