Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

318
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda