I have a Node.js server that is running over HTTP on port 8080. I've added a WebSocket server to handle requests requiring real-time data feed. It works fine on my local machine, but when I upload it to my EC2 instance, it throws a connect ECONNREFUSED
error when trying to connect to the WebSocket server. HTTP works fine using http://api.reservedname.com. But when using ws://api.reservedname.com it does not work.
This is the code that I am using to create the server.
app.js
import { WebSocketServer } from 'ws';
import express from 'express';
/* Init Express Framework */
const app = express();
/* Set the PORT and Start Web Server */
app.set('port', (process.env.PORT || 8083));
const server = http.createServer(app);
const wss = new WebSocketServer({ server });
wss.on('connection', (ws) => {
ws.on('error', console.error);
ws.on('message', (data) => {
console.log('Message Received: ', data);
});
});
I tried setting the WebSocket server on another port