Skip to content

Socket io doesn't do multiplexing if I share the options object between instances. #3898

Closed
@adrianlocurcio

Description

@adrianlocurcio

Describe the bug
Socket io doesn't do multiplexing when I share the same reference object as options between io instances, it creates 2 different websocket requests.

To Reproduce

  1. Create a basic socket-io server with 2 namesapces
  2. Create a basic webpage with socket-io from cdn (or npm) and try to connect to those 2 namespaces at the same time.

Socket.IO server version: 4.0.1

Server

const server = require("http").createServer();
const io = require("socket.io")(server, {
  transports: ["websocket"],
});

io.of("/matchmaking").on("connection", () => {
  console.log("connected to matchmaking namespace");
});

io.of("/lobby").on("connection", () => {
  console.log("connected to lobby namespace");
});
server.listen(5002, () => console.log("listening"));

Socket.IO client version: 4.0.1

Client

    <script>
      const opts = {
        transports: ["websocket"],
        autoConnect: false,
      };

      const socket1 = io("http://localhost:5002/lobby", opts);

      const socket2 = io("http://localhost:5002/matchmaking", opts);

      socket1.connect();

      socket2.connect();
    </script>

Expected behavior
Only 1 connection should established if I pass the same reference object to 2nd param on io call.

Platform:

  • Server: Node v14.16.0
  • Client: Chrome 89.0.4389.114 (Official Build) (64-bit)

Additional context
If you clone the opts object instead of sharing it between io instances it works properly, example:

const socket1 = io("http://localhost:5002/lobby", {...opts });
const socket2 = io("http://localhost:5002/matchmaking", {...opts});

Full working example:

<script>
      const opts = {
        transports: ["websocket"],
        autoConnect: false,
      };

      const socket1 = io("http://localhost:5002/lobby", {...opts}); // Cloning the opts object it works, very weird...

      const socket2 = io("http://localhost:5002/matchmaking", {...opts}); // Cloning the opts object it works, very weird...

      socket1.connect();

      socket2.connect();
    </script>

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions