Websocket error (Node js)

I am unable to connect to my websocket the error i get is Failed to execute ‘send’ on ‘WebSocket’: Still in CONNECT

i am trying to send a message to my websocket and it won’t load

    const socket = new WebSocket('ws://billowing-thunder-1177.fly.dev');
    socket.addEventListener('open', function (event) {
        console.log('Connected to WS Server')
    });
   socket.send("Hello");
const express = require('express')
const app = express()
const server = require('http').createServer(app);
const WebSocket = require('ws');
const port = process.env.PORT || 3000;
const wss = new WebSocket.Server({ server:server });


wss.on('connection', function connection(ws) {
  console.log('A new client Connected!');
  ws.send('Welcome New Client!');
  ws.on('message', function incoming(message) {
    console.log('received: %s', message);
    wss.clients.forEach(function each(client) {
      if (client !== ws && client.readyState === WebSocket.OPEN) {
        client.send(message);
      }
    });
    
  });
});
app.listen(port, () => console.log(`HelloNode app listening on port ${port}!`))

it works on localhost but not on fly.io

full error
WebSocket connection to ‘ws://billowing-thunder-1177.fly.dev/’ failed:

Some things to try:

  • Check that the internal port in the services section of fly.toml is set to the port the express application is listening on (either the PORT env var or 3000 if that’s not set anywhere). App Configuration (fly.toml) · Fly Docs
  • See what logs show up for your app after it start either in the Monitoring tab for the app or with fly logs
  • fly ssh console into the app and try making requests to the service (e.g., using curl -vs http://localhost:$PORT or wget). If that’s working connecting and returning upgrade responses, then it’s likely a fly.toml configuration issue or maybe a bug! If it’s not working, then something in the application is not running as expected when deployed to Fly.

Did you try wss: ?

1 Like