How to host websockets?
This is my first time hosting socket server, how can i do that on railway? I docs there are only webhooks which are a unknown field for me, my app uses socket.io is it possible to host that here?
project-id: 9ca93930-ec44-4d40-81ae-5a2f6b7c6928
Solution:Jump to solution
look at this example code from socket.io
https://socket.io/docs/v4/server-initialization/#with-express
but just replace the
3000
with the port
constant from this code example
https://docs.railway.app/troubleshoot/fixing-common-errors#node--express...23 Replies
Project ID:
N/A
Yeah, SocketIO works fine
U just have to be careful with this
says you
Railway | Help Center
WebSocket Connections Disconnecting – Railway | Help Center
Steps to take to make sure that WebSockets are behaving as expected on the platform.
skill issue
pretty sure thats been fixed long ago, unless me and fp where talking about something else
"Users who want websocket support should use custom domains for now. The root cause of the disconnects is to an issue with how envoy is handling our wildcard domains. "
but regardless, it is still a very good idea to send ping/pongs and socket.io does this by default
I won't buy domain, what is my other option?
uh no you didnt read it, websockets are still very much possible with a railway domain
please do not jump to conclusions like that
and if websockets are possible socket.io is too, your socket.io server will need to listen on the railway provided
PORT
variableSolution
look at this example code from socket.io
https://socket.io/docs/v4/server-initialization/#with-express
but just replace the
3000
with the port
constant from this code example
https://docs.railway.app/troubleshoot/fixing-common-errors#node--expressAnd if you are using expressjs, the SocketIo instance needs to be started together with the expressjs server in your code
example:
I'm trying this now, but one thing I don't understand how can my websocket listen on the same port as my express, this is how my code looks like now
------------------------------------------
const port = process.env.PORT || 3000;
app.listen(port, "0.0.0.0");
const httpServer = createServer(app);
httpServer.listen(port);
const io = new Server(httpServer, {
cors: {
credentials: true,
origin: ["http://localhost:3000"],
},
});
------------------------------------------
^
I see, yes
Why are you using app.listen together with server.listen?
that's a good quesiton
i'm noob, i shouldn't use that?
it's the same?
Not really
it is a good question, i have linked some example code, please take a look
If u wanna use it together with socketIo, its easier when using a
Server
express instanceSo, I should remove app.listen? To be honest, until now i didn't even use createHttpserver nor server.listen until brody linked that example
everything worked fine in my localhost with only cons io = new Server
yep
alright, I'll try it that way