Trying to use sockets with socket.io, works locally but not when deployed
Im using node/express for backend with react frontend. I am trying to establish a socket.io connection between them (they are two different deployments in the same railway project)
When I run it locally on my machine, it works perfectly, there's communication and all good.
When I deploy it on railway, I get an error, seemingly because for some reason they cannot communicate.
Any help/ideas?
Ill attach the errors and my code in the next comments
18 Replies
Project ID:
01e35af8-66ad-42c2-b38f-dab885c67b21
01e35af8-66ad-42c2-b38f-dab885c67b21
Error message in chrome
Code for the backend
This is the frontend code
I dont think its a problem with the code, since it works perfectly in localhost
its the code, read this please https://docs.railway.app/troubleshoot/fixing-common-errors
Alright, so the PORT variable is 80 in the backend.
Im using that port for the express hosting
you are also trying to specify a port in the socket.io url, you can only access railway services from port 443, so you shouldnt have any port in the url
Alright, so should I aim it at 443, or just not specify a port?
And should my socket.io port be 443 in the backend httpServer.listen?
Alright, so should I aim it at 443, or just not specify a port?you are using https, so 443 is implied, correct, dont specify a port
And should my socket.io port be 443 in the backend httpServer.listen?nope, please read the entire doc page i linked
I had read it, but I'll read it again anyways, thanks!
no skim reading
Alright so what I can only conclude is that
"Thus, your web server must listen on host 0.0.0.0 and the port that Railway provides in the PORT environment variable."
Means that I can only have one service with one port, and all requests will be sent to the default port?
So i cant have my express app in the default port and then have the socket.io service on a different port
So from that, my only idea is to split the socket.io functionality into a separate service/github repo
Or you can make both work on the same port and in the same service, expressjs can use ur socketio instance
oh, you can?
interesting, ill give it a try, thanks!
expressjs will use it on yourip/socketio/ or something like that as your websocket connection endpoint, but normally socketio itself handles that for you afaik
atleast clientside while connecting to the socketio in the backend
sorry if my explanation is a bit bad, u better read the documentation lol
It kinda makes sense
either that, or split it into a completely different service I guess, that ONLY hosts the socket.io connections