#Cannot connect to Node WebSockets deployment - even with custom domain

10 messages · Page 1 of 1 (latest)

wraith spear
#

Very basic server:

import { createServer } from "http";
import { ChatMessage } from "@chat/types";
import { WebSocketServer } from "ws";

function onSocketError(err) {
  console.error(err);
}

const server = createServer();
const wss = new WebSocketServer({ noServer: true });

wss.on("connection", (ws, request) => {
  console.log("New connection to websocket");

  ws.on("error", console.error);

  ws.on("message", (data) => {
    const msg: ChatMessage = JSON.parse(data.toString());
    console.log(`Received message ${msg} from user ${msg.fromId}`);

    wss.clients.forEach((client) => {
      client.send(JSON.stringify(msg));
    });
  });
});

server.on("upgrade", function upgrade(request, socket, head) {
  console.log("Upgrading to websocket");

  socket.on("error", onSocketError);
  socket.removeListener("error", onSocketError);

  wss.handleUpgrade(request, socket, head, (ws) => {
    wss.emit("connection", ws, request);
  });
});

server.on("listening", () => {
  console.log("Listening on port 8080");
});

server.listen(8080);

I connect in the client with

const newWS = new WebSocket("ws://ws.mydomain.com:8080");

    newWS.onopen = () => {
      console.log("Connected to chat server");
    };

    newWS.onerror = (err) => console.error(err);

    newWS.onclose = () => {
      console.log("Disconnected from chat server");
    };

There is no error message but also no console.log() that a connection has been established. Neither on the client nor on the server.

uneven canyonBOT
#

Project ID: 9ebe90c5-ef7a-458e-b833-ac1fc55ce7bb

wraith spear
#

9ebe90c5-ef7a-458e-b833-ac1fc55ce7bb

sharp lark
#

const newWS = new WebSocket("ws://ws.mydomain.com:8080");

  1. you arent using wss as your schema
  2. you are specifying a port in the connection string when you shouldn't be
#

websockets does absolutely work on railway, if you dont belive me, check for yourself
wss://utilities.up.railway.app/ws

wraith spear
#

Hey Brody, thank you for getting back to me. I have changed the connection string and it's still not working. At least throwing an error this time.

websocat wss://ws.mydomain.com
websocat: WebSocketError: WebSocketError: Received unexpected status code (503 Service Unavailable)
websocat: error running

The server code is very basic - just what I posted above. It works fine in localhost so not sure what to change about it. The subdomain was set using the CNAME record provided by Railway. Interestingly, I have to explicitly define the port 8080 in order to connect to it in localhost (ws://localhost:8080). With the deployed version it works neither with nor without the port defined.

sharp lark
#

when running on railway you want to be listening on the environment variable PORT

wraith spear
#

Thank you, that solved it. 🙂 Is this a common code pattern when using a PAAS provider? I would've never thought about this as the problem. Maybe the build process could look out for this and throw an error 🙂

sharp lark
#

some PAAS platforms will auto scan your app for the port its listening on and update itself, railway doesn’t do this and in a way i guess expects you to read the documentation