0

I have a simple snippet on the front end as follows which I can verify is working. I can do this by changing the port to something other than 3000 and it will error. It is definitely finding the server at that port:

// Create WebSocket connection .. will error if I change the port
const socket = new WebSocket('ws://localhost:3000');
console.log('DEBUG: Web socket is up: ');

// Connection opened
socket.addEventListener('open', function (event) {
  socket.send('Hello Server!');
});

I am using ws-express on the server side as follows. This was the minimal example given in the NPM docs:

const expressWs = require('express-ws')(app);

app.ws('/echo', (ws, req) => {
  ws.on('message', (msg) => {
    ws.send(msg);
  });
});

However, the open event on the client never fires. I would like to send messages from the client to the server, but I assume, that I need an open event to fire first.

0 Answers0