Socket.io client initialization doesn’t seem to be working as documented (4.x)

My scoket.io server is hosted on virtualized hardware exposed at an endpoint of example.com/<id>/socket.io/

And socket.io by default will try to connect to example.com/socket.io/ so I need to customize the endpoint used. According to the docs for 4.x:

In the examples above, the client will connect to the main namespace. Using only the main namespace should be sufficient for most use cases, but you can specify the namespace with:

// same origin version
const socket = io("/admin");
// cross origin version
const socket = io("https://server-domain.com/admin");

So I went with:

const socket = io("/<id>/socket.io");

and also tried:

const socket = io("/<id>/socket.io/");

and

const socket = io("https://example.com/<id>/socket.io");

and

const socket = io("https://example.com/<id>/socket.io/");

But the failing polling messages show that it’s trying to connect to the default https://example.com/socket.io/.

I can verify that the socket server is running at the correct endpoint because visiting https://example.com/<id>/socket.io/ serves a message: {"code":0,"message":"Transport unknown"} and https://example.com/<id>/socket.io/socket.io.js serves Socket.IO v4.0.0

What can I do to further debug or fix this?

Edit: Tried with v3 and experienced the same result.

Answer

What you’re trying to do is to get Socket.IO to connect to a different path, not a namespace. In short, you should use the path option, like so:

// The string passed as the first argument is the Namespace to use,
// not the path to connect to.
const socket = io("/", {
  // Set the <id> to your actual path ID.
  path: "/<id>/socket.io/"
});

Read more about Socket.IO Namespaces here.