socket.io-client icon indicating copy to clipboard operation
socket.io-client copied to clipboard

Fix: #1544, subsequent connections to previously used namespaces will multiplex

Open sean256 opened this issue 3 years ago • 1 comments

  • Socket.nsp to public readonly
  • Manager will remove the namespace'd socket upon socket disconnect, thus allowing subsequent connections to the same namespace to multiplex.

The kind of change this PR does introduce

  • [x] a bug fix
  • [ ] a new feature
  • [ ] an update to the documentation
  • [x] a code change that improves performance (in rare cases)
  • [ ] other

Current behaviour

Details here: https://github.com/socketio/socket.io-client/issues/1544 TLDR: 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.

New behaviour

When a socket on a namespace is disconnected, the manager will remove it from it's internal nsp Record. Thus subsequent connects will no longer create a new manager.

This will also help with performance for use cases where dynamic namespaces are used, which could create an infinite number of managers and socket instances in their nsp Record.

NOTE: The current behavior where a new manager is created if a client attempts to make multiple active connections to the same namespace is still present.

Other information (e.g. related issues)

https://github.com/socketio/socket.io-client/issues/1514 https://github.com/socketio/socket.io-client/issues/1364 https://github.com/socketio/socket.io-client/issues/866 https://github.com/socketio/socket.io/issues/1956

sean256 avatar Jun 20 '22 06:06 sean256

Hi! Thanks for creating this pull request.

What happens if the first socket is reused? In your example:

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", {});

b.connect();

As a workaround, you should be able to use the Manager directly:

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

const manager = new Manager("wss://example.com");

const a = manager.socket("/namespace-a", {});
const b = manager.socket("/namespace-b", {});

// some time later
b.disconnect();

// some time later create a NEW connection to namespace-b
const newB = manager.socket("/namespace-b", {});
newB.connect(); // ensure it's still connected

darrachequesne avatar Jun 26 '22 07:06 darrachequesne