I am facing a strange problem. I have a express backend server. When I do not have a base URL for my backend, the ws calls are working fine:
But, if I add a base URL ( for example, adding /api/ as part of the url ), my connections fail:
This the part of my backend code:
const easyrtc = require("open-easyrtc"); // EasyRTC external module
// Start Express http server
const webServer = http.createServer(app);
const socketIo = require("socket.io")(webServer, {path: '/api/'}); // web socket external module
// Start Socket.io so it attaches itself to Express server
const socketServer = socketIo.listen(webServer, { "log level": 1 });
easyrtc.listen(app, socketServer, null, (err, rtcRef) => {
console.log("Initiated");
rtcRef.events.on(
"roomCreate",
(appObj, creatorConnectionObj, roomName, roomOptions, callback) => {
console.log("roomCreate fired! Trying to create: " + roomName);
appObj.events.defaultListeners.roomCreate(
appObj,
creatorConnectionObj,
roomName,
roomOptions,
callback
);
}
);
});
// Listen on port
webServer.listen(port, () => {
console.log("listening");
});
Why does this happen and how can I fix it? I have been at a loss on this for a week now.
https://socket.io/docs/v4/server-options/
The
/socket.io/
is default value forpath
option. If you use{path: '/api/'}
option, then you should callinstead of
ws://localhost:3000/api/socket.io/?EIO=3...