Skip to content

Subsequent new connections to previously closed namespaces results in new connections #1544

@sean256

Description

@sean256

Describe the bug
When using namespaces, new "channels" are created over a shared connection as expected.
But if a namespace calls socket.disconnect(); then later a new instance connects to the same namespace, a whole new connection will be made instead of multiplexing.

Before I get into it, I did find these existing issues:
#1514
#1364
#866
And finally the root cause: socketio/socket.io#1956

After researching all of that, I believe this is a bug or more likely an unintended behavior. I understand #1956 and why opening a new connection to the same currently connected namespace is a thing, BUT, I really do not think this should happen if the previous channel was CLOSED.

I have a use case where my server code is using dynamic namespaces with regex, and the client is connecting to these dynamic namespaces using id's of various documents the user could browser to. So if my user loads document A, then B, then A again, it creates a new underlying connection. Since we have hundreds of possible id's and dynamic namespaces, caching each possible socket is not practical. This is just silly.

To Reproduce

(this code is not using dynamic namespaces as mentioned above, but it still demonstrates the issue)

Socket.IO server version: 4.5.1

Server

import { Server } from "socket.io";

const io = new Server(3000, {});

io.of("/namespace-a", (socket) => {
	// some logic
});


io.of("/namespace-b", (socket) => {
	// some logic
});

Client

import { io } from "socket.io-client";

const a = io("/namespace-a", {});
const b = io("/namespace-b", {});

// some time later
b.disconnect();

// some time later create a NEW connection to namespace-b
const newB = io("/namespace-b", {});

Expected behavior
I expect all namespace channels to utilize a single connection.

Platform:

  • Device: Mac. Chrome & Safari
  • OS: MacOS & Docker Node 16.14.x

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions