Skip to content

Commit

Permalink
fix: ensure connections are properly multiplexed
Browse files Browse the repository at this point in the history
When passing the same `options` argument for two distinct Socket
instances, a new Manager was created:

```js
const opts = {};

const socket1 = io("/foo", opts);
const socket2 = io("/bar", opts);

console.log(socket1.io === socket2.io); // false
```

This bug was introduced by [1].

Note: the `options` argument is modified at the `socket.io-client`
level (path, query) and at the `engine.io-client` level (host, port),
which may not be optimal.

[1]: 7a0c2b5

Related: socketio/socket.io#3898
  • Loading branch information
darrachequesne committed May 6, 2021
1 parent 7e81e66 commit dd2a8fc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function lookup(

opts = opts || {};

const parsed = url(uri as string, opts.path);
const parsed = url(uri as string, opts.path || "/socket.io");
const source = parsed.source;
const id = parsed.id;
const path = parsed.path;
Expand Down
19 changes: 19 additions & 0 deletions test/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,25 @@ describe("connection", function () {
s2.disconnect();
});

it("should start two connections with different paths", () => {
const s1 = io("/", { path: "/foo" });
const s2 = io("/", { path: "/bar" });

expect(s1.io).to.not.be(s2.io);
s1.disconnect();
s2.disconnect();
});

it("should start a single connection with different namespaces", () => {
const opts = {};
const s1 = io("/foo", opts);
const s2 = io("/bar", opts);

expect(s1.io).to.be(s2.io);
s1.disconnect();
s2.disconnect();
});

it("should work with acks", (done) => {
const socket = io({ forceNew: true });
socket.emit("ack");
Expand Down

0 comments on commit dd2a8fc

Please sign in to comment.