title: WebSockets WS error during WebSocket handshake: Unexpected response code: 200
body: <p>trying to deploy my app with ws on Heroku. On my local machine everything worked out well but after deploying on Heroku I get a handshake error with response status 200</p>
  
  <p>Here's my server code: </p>
  
  <pre><code>const WebSocketServer = require('ws').Server;
  const moment = require('moment');
  const app = require('./app');
  
  const wss = new WebSocketServer({ app });
  
  const connections = new Set();
  
  wss.on('connection', (ws) =&gt; {
    connections.add(ws);
  
    ws.on('message', (message) =&gt; {
      const time = moment(new Date()).format('HH:mm');
      const messageData = {
        time,
        message,
      };
      for (const connection of connections) {
        connection.send(JSON.stringify(messageData));
      }
    });
  
    ws.on('close', () =&gt; {
      connections.delete(ws);
    });
   });
  </code></pre>
  
  <p>And my client code:</p>
  
  <pre><code>const HOST = location.origin.replace(/^http/, 'ws');
  const ws = new WebSocket(HOST);
  
  ws.onmessage = (e) =&gt; {
    //
  };
  
  messageForm.addEventListener('submit', (e) =&gt; {
    //
    ws.send(message);
    //
  });
  </code></pre>