-1

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

  • Firewall rules? HTTP is on port 80 by default. – tadman Aug 30 '23 at 20:49
  • Both "http://api.reservedname.com" and "ws://api.reservedname.com" connect to port 80. If "http" works, but not "ws", then there is probably a misconfigured reverse proxy. – gre_gor Aug 30 '23 at 21:42

0 Answers0