Skip to content

Manager.socket() re-uses a manually disconnected socket without reconnecting it #1460

@AntonNeld

Description

@AntonNeld

Describe the bug
When calling Manager.socket('/someNamespace') a second time after already having called it before, it will re-use the same socket. If that socket has been manually disconnected using Socket.disconnect(), it will be returned in a disconnected state.

To Reproduce

Please fill the following code example:

Socket.IO server version: 3.1.1

Server

const { Server } = require('socket.io');

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

io.of('/someNamespace').on('connection', (socket) => {
  console.log(`connect ${socket.id}`);

  socket.on('disconnect', () => {
    console.log(`disconnect ${socket.id}`);
  });
});

Socket.IO client version: 3.1.1

Client

const io = require('socket.io-client');

const manager = new io.Manager('ws://localhost:3000');

const firstSocket = manager.socket('/someNamespace');
firstSocket.on('connect', () => {
  console.log('Connected first time');
  firstSocket.disconnect();
  console.log(firstSocket.connected); // false

  const secondSocket = manager.socket('/someNamespace');
  secondSocket.on('connect', () => {
    console.log('Connected second time'); // Never happens
  });
});

Expected behavior
I expected Manager.socket('/someNamespace') to return a socket that connects to the server. Either the old socket that tries to connect again, or a fresh socket.

Additional context
The context is a single page application where not all namespaces are relevant to all "pages" in the application, so we disconnect from a page-specific namespace when leaving a page. The reason for explicitly creating a Manager instance is to avoid creating a new underlying connection when we reconnect to the namespace.

Our current workaround is to manually call Socket.open() after Manager.socket().

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