Silly WebSocket mistake

I recently was trying to add a websocket to an existing NodeJS Express app I had written. Here is the important code below:

 

const WebSocket = require('ws');
const app = express();
const server = http.createServer(app);
const wss = new WebSocket.Server({ server });

app.listen(8080, function listening() {
  console.log('Listening on %d', server.address().port);
})


I kept getting 502 errors from NGINX with this code when I tried to establish a connection. See if you can spot the error…

I was pretty dumb. You can’t haveĀ app listening anymore. Instead you have to haveĀ server. The correct code is:

server.listen(8080, function listening() {
  console.log('Listening on %d', server.address().port);
});

Leave a comment if this happened to you too… I will feel less dumb.

Leave a Reply

Your email address will not be published. Required fields are marked *