Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

315
Views
Unable to connect to WebSocket server on Heroku using JavaScript client

Ok, so I have written a websocket server in node.js using the "ws" module, below is my code.

const WebSocketServer = require('ws');

const wss = new WebSocketServer.Server({ port: 8080 })
 

wss.on("connection", ws => {
    console.log("new client connected");
    ws.on("message", data => {
        wss.clients.forEach(function each(client) {
            client.send(data);
            console.log('Sent: ' + data);
        });
    });
    ws.on("close", () => {
        console.log("the client has disconnected");
    });
    ws.onerror = function () {
        console.log("Some Error occurred")
    }
});
console.log("The WebSocket server is running on port 8080");

The server is working perfectly, and I can connect to it using new WebSocket("ws://localhost:8080"); locally. The problem is that when I try to host this on my Heroku app, the WebSocket object is detecting the server just fine, and there are no errors in the console.
The problem is that the WebSocket object is not able to connect to the server. When I inspect the object in the console, it shows the socket's ReadyState property as '0' (still in connecting state) for about five minutes. Then, the socket just closes on its own (ReadyState 3, closed). The weirdest thing to me is that it is not throwing any errors.

Can someone please explain what the heck is going on, im kind of losing my mind trying to figure this out.
All help is greatly appreciated.

Thanks,
Adi

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!