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
Percy13mo ago
Project ID: N/A
Medim
Medim13mo ago
Yeah, SocketIO works fine U just have to be careful with this
Brody
Brody13mo ago
says you
Medim
Medim13mo ago
skill issue
Brody
Brody13mo ago
pretty sure thats been fixed long ago, unless me and fp where talking about something else
BIROALTACC
BIROALTACC13mo 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
Brody13mo ago
but regardless, it is still a very good idea to send ping/pongs and socket.io does this by default
BIROALTACC
BIROALTACC13mo ago
I won't buy domain, what is my other option?
Brody
Brody13mo 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
Brody13mo 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
Medim13mo 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
BIROALTACC13mo 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
Brody13mo ago
^
BIROALTACC
BIROALTACC13mo ago
I see, yes
Want results from more Discord servers?
Add your server