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

Two connections to different namespaces are multiplexed

Open joostlubach opened this issue 6 years ago • 3 comments

You want to:

  • [x] report a bug
  • [ ] request a feature

Current behaviour

I might be wrong, but it seems that there's a bug in choosing whether to multiplex connections. I was trying to connect to two different namespaces simultaneously, and it was re-using my connection, resulting in the incorrect request URL when doing authentication.

I'm connecting to two namespaces:

  • /web (with no query string)
  • /app (with query string ?token=XXXX)

What happens is that upon getting a connection in /app, the request URL has no query string. In fact, if I change the connection to /web such that it takes a bogus query string (?token=HELLO), that ends up in the connection to /app.

Expected behaviour

I expect not to have any request information leaked to another connection. Ever.

Setup

This is all in NodeJS (client and server).

Other information (e.g. stacktraces, related issues, suggestions how to fix)

It seems clear what's going on. In your index.js, you have this line of code:

var sameNamespace = cache[id] && path in cache[id].nsps;
var newConnection = opts.forceNew || opts['force new connection'] ||
                      false === opts.multiplex || sameNamespace;

The last part of the expression would IMO more logical to be !sameNamespace. I.e. a new connection if you're NOT using the same namespace.

But, I might be completely misunderstanding this logic. In any case, the workaround for me is to use forceNew (which IMHO should be the default anyway, but I can see how that's difficult to change without breaking legacy code).

Apart from this, even when multiplexing connections, never ever should information from one connection leak to another one. But that's for you guys to find out.

Hope this helps!

joostlubach avatar May 14 '20 21:05 joostlubach

I am facing the same issue in anglular

7abhik avatar Nov 26 '20 12:11 7abhik

For future readers:

query is an option of the Manager instance: https://socket.io/docs/v3/client-initialization/#query

Which means:

const socket1 = io(); // create a new manager
const socket2 = io("/namespace-a", {
  // query will be ignored, since we are reusing the first manager
  query: { 
    a: 42
  }
});
const socket3 = io("/namespace-b", {
  forceNew: true,
  // query will be taken in account, since we are forcing the creation of a new manager
  query: { 
    a: 42
  }
});

darrachequesne avatar Feb 02 '21 23:02 darrachequesne

@darrachequesne I just spent countless hours trying to fix a bug (query from 1st namespace was reused on a 2nd namespace) i thought i had in my codebase only to realize that it was just a flag in sockets.io.client. People, you have to understand this is critical information. A user that develops an app might use 20-30 3rd party dependencies. He cannot just know every tiny detail for each dependency!!!!

tomeof avatar Mar 20 '23 13:03 tomeof