0

I have tried setting up a namespace on the backend,

const server = require("http").createServer(app);
const connectedUsers = {};

const io = require("socket.io")(server, {
  path: "/socket",
  serveClient: false,
  // below are engine.IO options
  pingInterval: 10000,
  pingTimeout: 5000,
  cookie: false
});

const singularConnection = io.of("/singular-socket");

singularConnection.on("connection", socket => {
    console.log("unique user connected with socket ID " + socket);
}

And on my client, I try connecting with,

    const socket = io(GATEWAY, {
      path: "/socket/singular-socket",
      transports: ["websocket"],
      jsonp: false
    });

    socket.connect();

    socket.on("connect", () => {
      console.log("connected to socket server");
    });

I've tried different variation of the URL, getting rid of /socket and moving other stuff around, but I can't seem to get it working. What am I doing wrong here?

1 Answer 1

5

I don't have any experience in using socket.io, but from the docs...

To connect to a namespace, the client code would look like.

const socket = io('http://localhost/admin', {
  path: '/mypath'
});

Here, the socket connects to the admin namespace, with the custom path mypath.

The request URLs will look like: localhost/mypath/?EIO=3&transport=polling&sid= (the namespace is sent as part of the payload).

Following the above lines, your code should look like..

 const socket = io("http://localhost/singular-socket", {
      path: "/socket",
      transports: ["websocket"],
      jsonp: false
    })

Where /singular-socket is the namespace and /socket is the path.

Try this repl

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.