title: Nodejs and WebSockets Failed to construct 'WebSocket': The subprotocol '[object Object]' is invalid
body: <p>So my main webpage example.com is run through cloudflare and has a edge certificate from <code>Origin -&gt; CF Edge</code> (origin cert) and an edge certificate <code>CF Edge -&gt; Client</code>. I'm bypassing this for my websocket by using a subdomain, <code>dt.example.com</code> which uses cloudflare although using this link resolves the origin server address rather than the cloudflare edge server address (which i've found solves some issues for me, or maybe it's counterproductive to use this method?). Anyway, I am using a free ssl certificate for secure websockets -</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>'use strict';
  var https = require('https');
  var fs= require('fs');
  var express = require('express');
  var WebSocket = require('ws');
  
  var server = https.createServer({
      cert: fs.readFileSync('../ssl/wss/new/certificate.crt'),
      key: fs.readFileSync('../ssl/wss/new/private.pem'),
      ca: fs.readFileSync('../ssl/wss/new/ca_bundle.crt')
  });
  
  const wss = new WebSocket.Server({server});
  
  wss.on('connection', function connection (ws) {
    ws.on('message', function message (msg) {
      console.log(msg);
    });
  });
  
  server.listen(58443, function listening () {
      const ws = new WebSocket('wss://dt.example.com:58443', {
          rejectUnauthorized: false
      });
      ws.on('open', function open () {
          ws.send('Workin baby');
      });
  });</code></pre>
  </div>
  </div>
  </p>
  
  <p>-and using a websocket client plugin for chrome allows me to connect to the socket, however i cannot on example.com/webpage where the follow code is:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var ws = new window.WebSocket('wss://dt.example.com:58443/', {
      rejectUnauthorized: false
  });
  ws.on('open', function open () {
      ws.send('What\'s crackin?');
  });</code></pre>
  </div>
  </div>
  </p>
  
  <p>Although in browser (Chrome) when the client script executes it exits with the following error <code>Uncaught DOMException: Failed to construct 'WebSocket': The subprotocol '[object Object]' is invalid. (on line 1)</code></p>