R
Railway14mo 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
Percy14mo ago
Project ID: N/A
lasse
lasseOP14mo ago
N/A
Fragly
Fragly14mo ago
You should listen on the port railway provides to you ( env.PORT )
lasse
lasseOP14mo ago
alright so const wsServer = new ws.Server({ port: process.env.PORT});
Fragly
Fragly14mo ago
ya
lasse
lasseOP14mo ago
Alright let me test it out
Fragly
Fragly14mo 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
Brody14mo ago
what language does the syntax for env.PORT come from??
Fragly
Fragly14mo ago
I made it up <:chad_moment:758939193967378442>
Brody
Brody14mo ago
thats confusing
Fragly
Fragly14mo ago
PORT service variable*** weariest
Brody
Brody14mo 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
Fragly14mo ago
That is good to know, thank you brody
Brody
Brody14mo 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
lasseOP14mo ago
It's still wan't connect
lasse
lasseOP14mo ago
WebSocket connection to 'wss://websocket.orabot.xyz:8080/' failed and i have set my domain:
No description
Fragly
Fragly14mo ago
remove the port specification at the end of the url you're not listening on that port
lasse
lasseOP14mo ago
Thanks, let me test it It works now, thank you!
Fragly
Fragly14mo ago
no problem :D
Want results from more Discord servers?
Add your server