a year ago
I am creating a messaging app project and this is the front end code I have to connect to the railway node js backend server and I am not sure why it won't connect. It was connecting just fine when on local host but now I changed it to the railway production URL with the port number specified in the environment variable it does not work anymore. I don't know what is wrong.
if (!socketRef.current) {
console.log("Initializing new socket connection");
let socketServerURL = "wss://[nodejs-production-49b7.up.railway.app/:3001](nodejs-production-49b7.up.railway.app/:3001)";
socketRef.current = io(socketServerURL, {
transports: ["websocket", "polling"],
query: { chatId: selectedId, userId: currentUserID },
});
} else {
console.log("Reusing existing socket connection, updating query parameters");
[socketRef.current.io](socketRef.current.io).opts.query = { chatId: selectedId, userId: currentUserID };
}
3 Replies
a year ago
Nvm I chnaged it to
let socketServerURL = "wss://nodejs-production-49b7.up.railway.app:3001"; (removed the / before the ":" ) and it connects but now it is giving
failed: WebSocket is closed before the connection is established.
a year ago
Websockets is http, and you can only expose port 443 so your socket URL would be wss://nodejs-production-49b7.up.railway.app (not specifying a port here since 443 js implied when using wss://)
Status changed to Solved railway[bot] • about 1 year ago
a year ago
Thank you so much!!! I also found i had both server.listen and app.listen on my node js backend as well when i shoudl've only had the server.listen. It works now!!!