I am trying to test my websocket on Railway localy, so I can connect it to vercel
Anonymous
HOBBYOP

2 months ago

I am trying to test my websocket server and when I try to localy connect to it a get this error.

requestId:

"ZmFsGgQsRliUTmOjjUJq2g"

timestamp:

"2026-01-26T08:09:42.399090491Z"

method:

"GET"

path:

"/"

host:

"pleasing-love-production-1ecc.up.railway.app"

httpStatus:

502

upstreamProto:

""

downstreamProto:

"HTTP/1.1"

responseDetails:

"Retried single replica"

totalDuration:

3209

upstreamAddress:

""

clientUa:

"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36"

upstreamRqDuration:

3013

txBytes:

109

rxBytes:

599

srcIp:

"77.236.222.22"

edgeRegion:

"europe-west4-drams3a"

upstreamErrors:

"[{"deploymentInstanceID":"188c715a-9063-4047-854f-732d2b5ba82d","duration":1008,"error":"connection closed unexpectedly"},{"deploymentInstanceID":"188c715a-9063-4047-854f-732d2b5ba82d","duration":1003,"error":"connection closed unexpectedly"},{"deploymentInstanceID":"188c715a-9063-4047-854f-732d2b5ba82d","duration":1002,"error":"connection closed unexpectedly"}]"

$10 Bounty

1 Replies

dheya0
FREE

a month ago

Application Not Listening on the Railway-Assigned Port (Most Common)

Railway dynamically assigns the listening port via the PORT environment variable.

Incorrect:

server.listen(3000);

Correct:

const PORT = process.env.PORT || 3000;
server.listen(PORT, "0.0.0.0");

If the application does not bind to this port, Railway cannot forward traffic to it.

2. WebSocket Server Not Attached to an HTTP Server

Railway requires WebSocket traffic to be handled through an existing HTTP server.

Incorrect (breaks Railway routing):

new WebSocketServer({ port: 3000 });

Correct (Railway-compatible):

import http from "http";
import { WebSocketServer } from "ws";

const server = http.createServer();
const wss = new WebSocketServer({ server });

wss.on("connection", (ws) => {
  ws.send("Connected");
});

server.listen(process.env.PORT);

3. Invalid WebSocket Client Request

Opening the application URL directly in a browser sends a plain HTTP GET request, not a WebSocket upgrade request.

Incorrect:

https://your-app.up.railway.app

Correct:

new WebSocket("wss://your-app.up.railway.app");

Only clients that send proper Upgrade: websocket headers can establish a WebSocket connection.

4. Using ws:// Instead of wss:// in Production

Railway enforces HTTPS at the edge.

Incorrect:

ws://your-app.up.railway.app

Correct:

wss://your-app.up.railway.app

Loading...