Why does my web socket call fail when server has a base url?

298 Views Asked by At

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:

enter image description here

But, if I add a base URL ( for example, adding /api/ as part of the url ), my connections fail:

enter image description here

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.

1

There are 1 best solutions below

0
On

https://socket.io/docs/v4/server-options/

The /socket.io/ is default value for path option. If you use {path: '/api/'} option, then you should call

ws://localhost:3000/api/?EIO=3....

instead of ws://localhost:3000/api/socket.io/?EIO=3...