R
Railway9mo ago
lasse

Web Socket Wan't Connect

So i have basic websock server running in a simple node.js project, but in production i says it can't connect. I have wss://mydomain:<port>/ and added my custom domain. But still it wan't let me do it. I will provide the basic websocket server below. It works fine on my local computer.
const wsServer = new ws.Server({ port: 8080 });

wsServer.on("listening", () => {
Logger.debug("Websocket server is listening on port 8080");
});

wsServer.on("connection", (socket) => {
Logger.debug("New websocket connection", socket);

socket.on("close", () => {
Logger.debug("Websocket connection closed");
});
});
const wsServer = new ws.Server({ port: 8080 });

wsServer.on("listening", () => {
Logger.debug("Websocket server is listening on port 8080");
});

wsServer.on("connection", (socket) => {
Logger.debug("New websocket connection", socket);

socket.on("close", () => {
Logger.debug("Websocket connection closed");
});
});
19 Replies
Percy
Percy9mo ago
Project ID: N/A
lasse
lasse9mo ago
N/A
Fragly
Fragly9mo ago
You should listen on the port railway provides to you ( env.PORT )
lasse
lasse9mo ago
alright so const wsServer = new ws.Server({ port: process.env.PORT});
Fragly
Fragly9mo ago
ya
lasse
lasse9mo ago
Alright let me test it out
Fragly
Fragly9mo ago
i'd recommend doing this though
const port = process.env.PORT || 3000
console.log(`Running on port ${port}`)
const port = process.env.PORT || 3000
console.log(`Running on port ${port}`)
that way if no PORT is defined it defaults to 3000 or whatever
Brody
Brody9mo ago
what language does the syntax for env.PORT come from??
Fragly
Fragly9mo ago
I made it up <:chad_moment:758939193967378442>
Brody
Brody9mo ago
thats confusing
Fragly
Fragly9mo ago
PORT service variable*** weariest
Brody
Brody9mo ago
PORT environment variable variables set in the service are service variables, during build they are called build arguments, during runtime they are environment variables
Fragly
Fragly9mo ago
That is good to know, thank you brody
Brody
Brody9mo ago
(unless you set a PORT service variable yourself, the PORT variable generated by railway is only available at runtime as an environment variable)
lasse
lasse9mo ago
It's still wan't connect
lasse
lasse9mo ago
WebSocket connection to 'wss://websocket.orabot.xyz:8080/' failed and i have set my domain:
No description
Fragly
Fragly9mo ago
remove the port specification at the end of the url you're not listening on that port
lasse
lasse9mo ago
Thanks, let me test it It works now, thank you!
Fragly
Fragly9mo ago
no problem :D