Skip to content

Commit 129c641

Browse files
committedOct 21, 2020
feat: make Socket#join() and Socket#leave() synchronous
Depending on the adapter, Socket#join() may return: - nothing (in-memory and Redis adapters) - a promise (custom adapters) Breaking change: Socket#join() and Socket#leave() do not accept a callback argument anymore. Before: ```js socket.join("room1", () => { io.to("room1").emit("hello"); }); ``` After: ``` socket.join("room1"); io.to("room1").emit("hello"); // or await socket.join("room1"); for custom adapters ``` Note: the need for an asynchronous method came from the Redis adapter, which did override the Adapter#add() method in earlier versions, but this is not the case anymore. Reference: - https://github.com/socketio/socket.io/blob/2.3.0/lib/socket.js#L236-L258 - https://github.com/socketio/socket.io-adapter/blob/1.1.2/index.js#L56-L65 - socketio/socket.io-redis-adapter@05f926e Related: #3662
1 parent 0d74f29 commit 129c641

File tree

3 files changed

+67
-104
lines changed

3 files changed

+67
-104
lines changed
 

‎lib/socket.ts

+7-16
Original file line numberDiff line numberDiff line change
@@ -238,38 +238,29 @@ export class Socket extends EventEmitter {
238238
* Joins a room.
239239
*
240240
* @param {String|Array} rooms - room or array of rooms
241-
* @param {Function} fn - optional, callback
242-
* @return {Socket} self
241+
* @return a Promise or nothing, depending on the adapter
243242
* @public
244243
*/
245-
public join(rooms: Room | Array<Room>, fn?: (err: Error) => void): Socket {
246-
debug("joining room %s", rooms);
244+
public join(rooms: Room | Array<Room>): Promise<void> | void {
245+
debug("join room %s", rooms);
247246

248-
this.adapter.addAll(
247+
return this.adapter.addAll(
249248
this.id,
250249
new Set(Array.isArray(rooms) ? rooms : [rooms])
251250
);
252-
debug("joined room %s", rooms);
253-
fn && fn(null);
254-
return this;
255251
}
256252

257253
/**
258254
* Leaves a room.
259255
*
260256
* @param {String} room
261-
* @param {Function} fn - optional, callback
262-
* @return {Socket} self
257+
* @return a Promise or nothing, depending on the adapter
263258
* @public
264259
*/
265-
public leave(room: string, fn?: (err: Error) => void): Socket {
260+
public leave(room: string): Promise<void> | void {
266261
debug("leave room %s", room);
267-
this.adapter.del(this.id, room);
268262

269-
debug("left room %s", room);
270-
fn && fn(null);
271-
272-
return this;
263+
return this.adapter.del(this.id, room);
273264
}
274265

275266
/**

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"base64id": "~2.0.0",
3939
"debug": "~4.1.0",
4040
"engine.io": "~4.0.0",
41-
"socket.io-adapter": "2.0.3-rc1",
41+
"socket.io-adapter": "2.0.3-rc2",
4242
"socket.io-parser": "4.0.1-rc2"
4343
},
4444
"devDependencies": {

‎test/socket.io.ts

+59-87
Original file line numberDiff line numberDiff line change
@@ -488,10 +488,9 @@ describe("socket.io", () => {
488488
const socket = client(srv);
489489

490490
sio.on("connection", s => {
491-
s.join("a", () => {
492-
// FIXME not sure why process.nextTick() is needed here
493-
process.nextTick(() => s.disconnect());
494-
});
491+
s.join("a");
492+
// FIXME not sure why process.nextTick() is needed here
493+
process.nextTick(() => s.disconnect());
495494

496495
let total = 2;
497496
s.on("disconnecting", reason => {
@@ -578,22 +577,19 @@ describe("socket.io", () => {
578577
let total = 3;
579578
sio.of("/chat").on("connection", socket => {
580579
if (chatIndex++) {
581-
socket.join("foo", () => {
582-
chatFooSid = socket.id;
583-
--total || getSockets();
584-
});
580+
socket.join("foo");
581+
chatFooSid = socket.id;
582+
--total || getSockets();
585583
} else {
586-
socket.join("bar", () => {
587-
chatBarSid = socket.id;
588-
--total || getSockets();
589-
});
584+
socket.join("bar");
585+
chatBarSid = socket.id;
586+
--total || getSockets();
590587
}
591588
});
592589
sio.of("/other").on("connection", socket => {
593-
socket.join("foo", () => {
594-
otherSid = socket.id;
595-
--total || getSockets();
596-
});
590+
socket.join("foo");
591+
otherSid = socket.id;
592+
--total || getSockets();
597593
});
598594
});
599595
async function getSockets() {
@@ -623,22 +619,19 @@ describe("socket.io", () => {
623619
let total = 3;
624620
sio.of("/chat").on("connection", socket => {
625621
if (chatIndex++) {
626-
socket.join("foo", () => {
627-
chatFooSid = socket.id;
628-
--total || getSockets();
629-
});
622+
socket.join("foo");
623+
chatFooSid = socket.id;
624+
--total || getSockets();
630625
} else {
631-
socket.join("bar", () => {
632-
chatBarSid = socket.id;
633-
--total || getSockets();
634-
});
626+
socket.join("bar");
627+
chatBarSid = socket.id;
628+
--total || getSockets();
635629
}
636630
});
637631
sio.of("/other").on("connection", socket => {
638-
socket.join("foo", () => {
639-
otherSid = socket.id;
640-
--total || getSockets();
641-
});
632+
socket.join("foo");
633+
otherSid = socket.id;
634+
--total || getSockets();
642635
});
643636
});
644637
async function getSockets() {
@@ -1711,20 +1704,6 @@ describe("socket.io", () => {
17111704
});
17121705
});
17131706

1714-
it("should always trigger the callback (if provided) when joining a room", done => {
1715-
const srv = createServer();
1716-
const sio = new Server(srv);
1717-
1718-
srv.listen(() => {
1719-
const socket = client(srv);
1720-
sio.on("connection", s => {
1721-
s.join("a", () => {
1722-
s.join("a", done);
1723-
});
1724-
});
1725-
});
1726-
});
1727-
17281707
it("should throw on reserved event", done => {
17291708
const srv = createServer();
17301709
const sio = new Server(srv);
@@ -1861,13 +1840,13 @@ describe("socket.io", () => {
18611840
socket1.on("a", () => {
18621841
done();
18631842
});
1864-
socket1.emit("join", "woot", () => {
1865-
socket1.emit("emit", "woot");
1866-
});
1843+
socket1.emit("join", "woot");
1844+
socket1.emit("emit", "woot");
18671845

18681846
sio.on("connection", socket => {
18691847
socket.on("join", (room, fn) => {
1870-
socket.join(room, fn);
1848+
socket.join(room);
1849+
fn && fn();
18711850
});
18721851

18731852
socket.on("emit", room => {
@@ -1904,7 +1883,8 @@ describe("socket.io", () => {
19041883

19051884
sio.on("connection", socket => {
19061885
socket.on("join", (room, fn) => {
1907-
socket.join(room, fn);
1886+
socket.join(room);
1887+
fn && fn();
19081888
});
19091889

19101890
socket.on("emit", room => {
@@ -1949,7 +1929,8 @@ describe("socket.io", () => {
19491929

19501930
sio.on("connection", socket => {
19511931
socket.on("join", (room, fn) => {
1952-
socket.join(room, fn);
1932+
socket.join(room);
1933+
fn && fn();
19531934
});
19541935

19551936
socket.on("broadcast", () => {
@@ -1996,7 +1977,8 @@ describe("socket.io", () => {
19961977

19971978
sio.on("connection", socket => {
19981979
socket.on("join", (room, fn) => {
1999-
socket.join(room, fn);
1980+
socket.join(room);
1981+
fn && fn();
20001982
});
20011983
socket.on("broadcast", () => {
20021984
socket.broadcast.to("test").emit("bin", Buffer.alloc(5));
@@ -2013,21 +1995,17 @@ describe("socket.io", () => {
20131995
srv.listen(() => {
20141996
const socket = client(srv);
20151997
sio.on("connection", s => {
2016-
s.join("a", () => {
2017-
expect(s.rooms).to.contain(s.id, "a");
2018-
s.join("b", () => {
2019-
expect(s.rooms).to.contain(s.id, "a", "b");
2020-
s.join("c", () => {
2021-
expect(s.rooms).to.contain(s.id, "a", "b", "c");
2022-
s.leave("b", () => {
2023-
expect(s.rooms).to.contain(s.id, "a", "c");
2024-
s.leaveAll();
2025-
expect(s.rooms.size).to.eql(0);
2026-
done();
2027-
});
2028-
});
2029-
});
2030-
});
1998+
s.join("a");
1999+
expect(s.rooms).to.contain(s.id, "a");
2000+
s.join("b");
2001+
expect(s.rooms).to.contain(s.id, "a", "b");
2002+
s.join("c");
2003+
expect(s.rooms).to.contain(s.id, "a", "b", "c");
2004+
s.leave("b");
2005+
expect(s.rooms).to.contain(s.id, "a", "c");
2006+
s.leaveAll();
2007+
expect(s.rooms.size).to.eql(0);
2008+
done();
20312009
});
20322010
});
20332011
});
@@ -2039,13 +2017,11 @@ describe("socket.io", () => {
20392017
srv.listen(() => {
20402018
const socket = client(srv);
20412019
sio.on("connection", s => {
2042-
s.join("a", () => {
2043-
expect(s.nsp.adapter.rooms).to.contain("a");
2044-
s.leave("a", () => {
2045-
expect(s.nsp.adapter.rooms).to.not.contain("a");
2046-
done();
2047-
});
2048-
});
2020+
s.join("a");
2021+
expect(s.nsp.adapter.rooms).to.contain("a");
2022+
s.leave("a");
2023+
expect(s.nsp.adapter.rooms).to.not.contain("a");
2024+
done();
20492025
});
20502026
});
20512027
});
@@ -2057,18 +2033,15 @@ describe("socket.io", () => {
20572033
srv.listen(() => {
20582034
const socket = client(srv);
20592035
sio.on("connection", s => {
2060-
s.join("a", () => {
2061-
expect(s.rooms).to.contain(s.id, "a");
2062-
s.join("b", () => {
2063-
expect(s.rooms).to.contain(s.id, "a", "b");
2064-
s.leave("unknown", () => {
2065-
expect(s.rooms).to.contain(s.id, "a", "b");
2066-
s.leaveAll();
2067-
expect(s.rooms.size).to.eql(0);
2068-
done();
2069-
});
2070-
});
2071-
});
2036+
s.join("a");
2037+
expect(s.rooms).to.contain(s.id, "a");
2038+
s.join("b");
2039+
expect(s.rooms).to.contain(s.id, "a", "b");
2040+
s.leave("unknown");
2041+
expect(s.rooms).to.contain(s.id, "a", "b");
2042+
s.leaveAll();
2043+
expect(s.rooms.size).to.eql(0);
2044+
done();
20722045
});
20732046
});
20742047
});
@@ -2080,10 +2053,9 @@ describe("socket.io", () => {
20802053
srv.listen(() => {
20812054
const socket = client(srv);
20822055
sio.on("connection", s => {
2083-
s.join(["a", "b", "c"], () => {
2084-
expect(s.rooms).to.contain(s.id, "a", "b", "c");
2085-
done();
2086-
});
2056+
s.join(["a", "b", "c"]);
2057+
expect(s.rooms).to.contain(s.id, "a", "b", "c");
2058+
done();
20872059
});
20882060
});
20892061
});

0 commit comments

Comments
 (0)
Please sign in to comment.