WebSocket connection failed

colinlienardTRIAL

a year ago

I have just deployed a node.js websocket server to Railway and I cannot connect to it, I get the error: WebSocket connection to 'wss://....[up.railway.app/](up.railway.app/)' failed: without any other informations.
It works fine when I run it in local.

My server code:

import { WebSocketServer } from "ws";

const wss = new WebSocketServer({ port: 443 });

wss.on("connection", function connection(ws) {
  console.log("connected");

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

  ws.on("message", function message(data) {
    console.log("received: %s", data);
  });

  ws.send("something");
});

Client code:

const ws = new WebSocket('wss://....up.railway.app');

ws.onopen = function open() {
    console.log('connected');
};
Solved

3 Replies

a year ago

Both your websocket server and http server need to be listening on the same port, the port you want to be listening on would be the environment variable PORT that Railway sets for you behind the scenes, not 443. after you make that change no client side changes should be needed by the looks of things.


Status changed to Solved railway[bot] about 1 year ago


colinlienardTRIAL

a year ago

Thanks it does work now! I didn't look closely enough at the documentation


a year ago

Glad to hear!