R
Railway14mo ago
Supamap

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
Percy
Percy14mo ago
Project ID: 01e35af8-66ad-42c2-b38f-dab885c67b21
Supamap
SupamapOP14mo ago
01e35af8-66ad-42c2-b38f-dab885c67b21
Supamap
SupamapOP14mo ago
Error message in chrome
No description
Supamap
SupamapOP14mo ago
let app = server()

const httpServer = createServer();

export const io = new Server(httpServer, { cors:{origin:[process.env.SIMPLICAR_FRONT_URL]}, transports: ['websocket', 'polling'], addTrailingSlash: false, path: "/socket.io" });

io.on("connection", (socket) => {
console.log("SOCKET.IO ON CONNECTION")
});

httpServer.listen(4000, () => {
console.log(`Servidor de Socket.io escuchando en el puerto ${4000}`);
});
let app = server()

const httpServer = createServer();

export const io = new Server(httpServer, { cors:{origin:[process.env.SIMPLICAR_FRONT_URL]}, transports: ['websocket', 'polling'], addTrailingSlash: false, path: "/socket.io" });

io.on("connection", (socket) => {
console.log("SOCKET.IO ON CONNECTION")
});

httpServer.listen(4000, () => {
console.log(`Servidor de Socket.io escuchando en el puerto ${4000}`);
});
Code for the backend
const socket = io(process.env.NEXT_PUBLIC_SOCKETIO_ENDPOINT)


socket.on('plate-scrapper-updated', (plate) => {
console.log("Socket recived 1", plate)
updatePlateScrapper(plate)
console.log('Salio de updatePlateScrapper')

})
const socket = io(process.env.NEXT_PUBLIC_SOCKETIO_ENDPOINT)


socket.on('plate-scrapper-updated', (plate) => {
console.log("Socket recived 1", plate)
updatePlateScrapper(plate)
console.log('Salio de updatePlateScrapper')

})
This is the frontend code I dont think its a problem with the code, since it works perfectly in localhost
Brody
Brody14mo ago
Supamap
SupamapOP14mo ago
Alright, so the PORT variable is 80 in the backend. Im using that port for the express hosting
let app: Express = express();

const HOST = process.env.HOST || 'localhost';
const PORT = process.env.PORT || '3000';
const DBURI = process.env.MONGO_URI || ''

const link = "http://" + HOST + ":" + PORT.toString()

await InitializeMiddleWare.InitializeCommonMiddleware(app)
await InitializeRoutes.Initialize(app, link)
await InitializeMiddleWare.InitializeErrorHandlingMiddleware(app)
await InitializeDB.Connect(DBURI)

app.listen(PORT, () => {
logger.info(`Server started listening at ${HOST} on ${PORT} port.`)
})
let app: Express = express();

const HOST = process.env.HOST || 'localhost';
const PORT = process.env.PORT || '3000';
const DBURI = process.env.MONGO_URI || ''

const link = "http://" + HOST + ":" + PORT.toString()

await InitializeMiddleWare.InitializeCommonMiddleware(app)
await InitializeRoutes.Initialize(app, link)
await InitializeMiddleWare.InitializeErrorHandlingMiddleware(app)
await InitializeDB.Connect(DBURI)

app.listen(PORT, () => {
logger.info(`Server started listening at ${HOST} on ${PORT} port.`)
})
Brody
Brody14mo ago
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
Supamap
SupamapOP14mo ago
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?
Brody
Brody14mo ago
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
Supamap
SupamapOP14mo ago
I had read it, but I'll read it again anyways, thanks!
Brody
Brody14mo ago
no skim reading
Supamap
SupamapOP14mo ago
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
Medim
Medim14mo ago
Or you can make both work on the same port and in the same service, expressjs can use ur socketio instance
Supamap
SupamapOP14mo ago
oh, you can?
Medim
Medim14mo ago
const app = express();
var http = require("http");
var server = http.createServer(app);

const io = require("socket.io")(server, {
cors: {
origin: "*",
methods: ["GET", "POST"],
},
});
io.on("connection", async (socket) => {
console.log("Connected: ", socket.id);
});
});

server.listen(env.PORT);
const app = express();
var http = require("http");
var server = http.createServer(app);

const io = require("socket.io")(server, {
cors: {
origin: "*",
methods: ["GET", "POST"],
},
});
io.on("connection", async (socket) => {
console.log("Connected: ", socket.id);
});
});

server.listen(env.PORT);
Supamap
SupamapOP14mo ago
interesting, ill give it a try, thanks!
Medim
Medim14mo ago
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
Supamap
SupamapOP14mo ago
It kinda makes sense either that, or split it into a completely different service I guess, that ONLY hosts the socket.io connections
Want results from more Discord servers?
Add your server