Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

317
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda