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
Percy10mo ago
Project ID: N/A
Medim
Medim10mo ago
Yeah, SocketIO works fine U just have to be careful with this
Brody
Brody10mo ago
says you
Medim
Medim10mo ago
skill issue
Brody
Brody10mo ago
pretty sure thats been fixed long ago, unless me and fp where talking about something else
BIROALTACC
BIROALTACC10mo 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
Brody10mo ago
but regardless, it is still a very good idea to send ping/pongs and socket.io does this by default
BIROALTACC
BIROALTACC10mo ago
I won't buy domain, what is my other option?
Brody
Brody10mo 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
Brody10mo 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
Medim10mo 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
BIROALTACC10mo 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
Brody10mo ago
^
BIROALTACC
BIROALTACC10mo ago
I see, yes
Medim
Medim10mo ago
Why are you using app.listen together with server.listen?
BIROALTACC
BIROALTACC10mo ago
that's a good quesiton i'm noob, i shouldn't use that? it's the same?
Medim
Medim10mo ago
Not really
Brody
Brody10mo ago
it is a good question, i have linked some example code, please take a look
Medim
Medim10mo ago
If u wanna use it together with socketIo, its easier when using a Server express instance
BIROALTACC
BIROALTACC10mo 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
Medim10mo ago
yep
BIROALTACC
BIROALTACC10mo ago
alright, I'll try it that way