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:
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...
Jump to solution
23 Replies
Percy
Percy15mo ago
Project ID: N/A
Medim
Medim15mo ago
Yeah, SocketIO works fine U just have to be careful with this
Brody
Brody15mo ago
says you
Medim
Medim15mo ago
skill issue
Brody
Brody15mo ago
pretty sure thats been fixed long ago, unless me and fp where talking about something else
BIROALTACC
BIROALTACC15mo ago
"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. "
Brody
Brody15mo ago
but regardless, it is still a very good idea to send ping/pongs and socket.io does this by default
BIROALTACC
BIROALTACC15mo ago
I won't buy domain, what is my other option?
Brody
Brody15mo ago
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 variable
Solution
Brody
Brody15mo ago
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
Medim
Medim15mo ago
And if you are using expressjs, the SocketIo instance needs to be started together with the expressjs server in your code example:
var http = require("http");
var server = http.createServer(app);

const io = require("socket.io")(server, {
cors: {
origin: "*",
methods: ["GET", "POST"],
},
});
var http = require("http");
var server = http.createServer(app);

const io = require("socket.io")(server, {
cors: {
origin: "*",
methods: ["GET", "POST"],
},
});
BIROALTACC
BIROALTACC15mo ago
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"], }, }); ------------------------------------------
Brody
Brody15mo ago
^
BIROALTACC
BIROALTACC15mo ago
I see, yes
Medim
Medim15mo ago
Why are you using app.listen together with server.listen?
BIROALTACC
BIROALTACC15mo ago
that's a good quesiton i'm noob, i shouldn't use that? it's the same?
Medim
Medim15mo ago
Not really
Brody
Brody15mo ago
it is a good question, i have linked some example code, please take a look
Medim
Medim15mo ago
If u wanna use it together with socketIo, its easier when using a Server express instance
BIROALTACC
BIROALTACC15mo ago
So, 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
Medim
Medim15mo ago
yep
BIROALTACC
BIROALTACC15mo ago
alright, I'll try it that way
Want results from more Discord servers?
Add your server