title: Connecting front-end native websocket client to Node.js Socket.io server
body: <p>I am using Socket.io in my Express/Node.js app for the backend websocket server. The server side code looks like this:</p>
  
  <pre><code>const socketIo = io(server);
  socketIo.on('connection', (client) =&gt; {
      console.log('client connected');
      client.emit('test', { test: '123' });
      client.on('client-test', (data) =&gt; {
          console.log('from client: ', data);
      });
  });
  </code></pre>
  
  <p>On the front-end (hosted on the same Node.js server), I am using native websockets, I have this:</p>
  
  <pre><code>const socket = new WebSocket('ws://localhost:3001');
  
  socket.onopen = () =&gt; {
      socket.send('I am a client and I am listening');
  
  socket.onmessage = (event) =&gt; {
      console.log('Client received message: ', event);
  };
  
  socket.onclose = (event) =&gt; {
      console.log('Client socket has closed: ', event);
      }
  };
  </code></pre>
  
  <p>The websocket connection never seems to be established (I get nothing logged from the server). Furthermore, I'm not sure how to accepted the emitted message from the server on the client-side (i.e. the 'test' message) nor how to accepted the message on the server from the client (i.e. the server is expecting some 'client-test' message).</p>
  
  <p>I'm wondering if native websockets on the front-end are incompatible with Socket.io on the backend. I had my set-up working when using Socket.io on the front-end as well, but I would like to have it working with native websockets on the front-end if possible.</p>