Skip to content

Commit dd2a8fc

Browse files
fix: ensure connections are properly multiplexed
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
1 parent 7e81e66 commit dd2a8fc

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

lib/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ function lookup(
4747

4848
opts = opts || {};
4949

50-
const parsed = url(uri as string, opts.path);
50+
const parsed = url(uri as string, opts.path || "/socket.io");
5151
const source = parsed.source;
5252
const id = parsed.id;
5353
const path = parsed.path;

test/connection.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,25 @@ describe("connection", function () {
4040
s2.disconnect();
4141
});
4242

43+
it("should start two connections with different paths", () => {
44+
const s1 = io("/", { path: "/foo" });
45+
const s2 = io("/", { path: "/bar" });
46+
47+
expect(s1.io).to.not.be(s2.io);
48+
s1.disconnect();
49+
s2.disconnect();
50+
});
51+
52+
it("should start a single connection with different namespaces", () => {
53+
const opts = {};
54+
const s1 = io("/foo", opts);
55+
const s2 = io("/bar", opts);
56+
57+
expect(s1.io).to.be(s2.io);
58+
s1.disconnect();
59+
s2.disconnect();
60+
});
61+
4362
it("should work with acks", (done) => {
4463
const socket = io({ forceNew: true });
4564
socket.emit("ack");

0 commit comments

Comments
 (0)