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