Skip to content

Commit

Permalink
feat: remove deprecated methods
Browse files Browse the repository at this point in the history
An official alternative has been added in the core in v4.0.0:

- adapter.remoteJoin() -> io.socketsJoin()
- adapter.remoteLeave() -> io.socketsLeave()
- adapter.remoteDisconnect() -> io.disconnectSockets()
- adapter.sockets() -> io.fetchSockets()

BREAKING CHANGE: the remoteJoin(), remoteLeave(), remoteDisconnect()
and sockets() methods are removed in favor of the official alternatives

Related: socketio/socket.io@b25495c
  • Loading branch information
darrachequesne committed Dec 7, 2022
1 parent 5b3bc8a commit fb760d9
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 225 deletions.
188 changes: 0 additions & 188 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -663,53 +663,6 @@ export class RedisAdapter extends Adapter {
super.broadcastWithAck(packet, opts, clientCountCallback, ack);
}

/**
* @deprecated Please use `namespace.fetchSockets()` instead.
*
* Gets a list of sockets by sid.
*
* @param {Set<Room>} rooms the explicit set of rooms to check.
*/
public async sockets(rooms: Set<Room>): Promise<Set<SocketId>> {
const localSockets = await super.sockets(rooms);
const numSub = await this.getNumSub();
debug('waiting for %d responses to "sockets" request', numSub);

if (numSub <= 1) {
return Promise.resolve(localSockets);
}

const requestId = uid2(6);
const request = JSON.stringify({
uid: this.uid,
requestId,
type: RequestType.SOCKETS,
rooms: [...rooms],
});

return new Promise((resolve, reject) => {
const timeout = setTimeout(() => {
if (this.requests.has(requestId)) {
reject(
new Error("timeout reached while waiting for sockets response")
);
this.requests.delete(requestId);
}
}, this.requestsTimeout);

this.requests.set(requestId, {
type: RequestType.SOCKETS,
numSub,
resolve,
timeout,
msgCount: 1,
sockets: localSockets,
});

this.pubClient.publish(this.requestChannel, request);
});
}

/**
* Gets the list of all rooms (across every node)
*
Expand Down Expand Up @@ -754,147 +707,6 @@ export class RedisAdapter extends Adapter {
});
}

/**
* @deprecated Please use `namespace.socketsJoin()` instead.
*
* Makes the socket with the given id join the room
*
* @param {String} id - socket id
* @param {String} room - room name
* @public
*/
public remoteJoin(id: SocketId, room: Room): Promise<void> {
const requestId = uid2(6);

const socket = this.nsp.sockets.get(id);
if (socket) {
socket.join(room);
return Promise.resolve();
}

const request = JSON.stringify({
uid: this.uid,
requestId,
type: RequestType.REMOTE_JOIN,
sid: id,
room,
});

return new Promise((resolve, reject) => {
const timeout = setTimeout(() => {
if (this.requests.has(requestId)) {
reject(
new Error("timeout reached while waiting for remoteJoin response")
);
this.requests.delete(requestId);
}
}, this.requestsTimeout);

this.requests.set(requestId, {
type: RequestType.REMOTE_JOIN,
resolve,
timeout,
});

this.pubClient.publish(this.requestChannel, request);
});
}

/**
* @deprecated Please use `namespace.socketsLeave()` instead.
*
* Makes the socket with the given id leave the room
*
* @param {String} id - socket id
* @param {String} room - room name
* @public
*/
public remoteLeave(id: SocketId, room: Room): Promise<void> {
const requestId = uid2(6);

const socket = this.nsp.sockets.get(id);
if (socket) {
socket.leave(room);
return Promise.resolve();
}

const request = JSON.stringify({
uid: this.uid,
requestId,
type: RequestType.REMOTE_LEAVE,
sid: id,
room,
});

return new Promise((resolve, reject) => {
const timeout = setTimeout(() => {
if (this.requests.has(requestId)) {
reject(
new Error("timeout reached while waiting for remoteLeave response")
);
this.requests.delete(requestId);
}
}, this.requestsTimeout);

this.requests.set(requestId, {
type: RequestType.REMOTE_LEAVE,
resolve,
timeout,
});

this.pubClient.publish(this.requestChannel, request);
});
}

/**
* @deprecated Please use `namespace.disconnectSockets()` instead.
*
* Makes the socket with the given id to be forcefully disconnected
*
* @param {String} id - socket id
* @param {Boolean} close - if `true`, closes the underlying connection
*
* @public
*/
public remoteDisconnect(id: SocketId, close?: boolean): Promise<void> {
const requestId = uid2(6);

const socket = this.nsp.sockets.get(id);
if (socket) {
socket.disconnect(close);
return Promise.resolve();
}

const request = JSON.stringify({
uid: this.uid,
requestId,
type: RequestType.REMOTE_DISCONNECT,
sid: id,
close,
});

return new Promise((resolve, reject) => {
const timeout = setTimeout(() => {
if (this.requests.has(requestId)) {
reject(
new Error(
"timeout reached while waiting for remoteDisconnect response"
)
);
this.requests.delete(requestId);
}
}, this.requestsTimeout);

this.requests.set(requestId, {
type: RequestType.REMOTE_DISCONNECT,
resolve,
timeout,
});

this.pubClient.publish(this.requestChannel, request);
});
}

public async fetchSockets(opts: BroadcastOptions): Promise<any[]> {
const localSockets = await super.fetchSockets(opts);

Expand Down
37 changes: 0 additions & 37 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,18 +108,6 @@ describe(`socket.io-redis with ${
socket1.disconnect();
});

it("returns sockets in the same room", async () => {
socket1.join("woot");
socket2.join("woot");

const sockets = await namespace1.adapter.sockets(new Set(["woot"]));

expect(sockets.size).to.eql(2);
expect(sockets.has(socket1.id)).to.be(true);
expect(sockets.has(socket2.id)).to.be(true);
expect(namespace1.adapter.requests.size).to.eql(0);
});

it("broadcasts with multiple acknowledgements", (done) => {
client1.on("test", (cb) => {
cb(1);
Expand Down Expand Up @@ -266,31 +254,6 @@ describe(`socket.io-redis with ${
expect(rooms.has("woot1")).to.be(true);
});

it("makes a given socket join a room", async () => {
await namespace3.adapter.remoteJoin(socket1.id, "woot3");

expect(socket1.rooms.size).to.eql(2);
expect(socket1.rooms.has("woot3")).to.be(true);
});

it("makes a given socket leave a room", async () => {
socket1.join("woot3");

await namespace3.adapter.remoteLeave(socket1.id, "woot3");

expect(socket1.rooms.size).to.eql(1);
expect(socket1.rooms.has("woot3")).to.be(false);
});

it("makes a given socket disconnect", (done) => {
client1.on("disconnect", (err) => {
expect(err).to.be("io server disconnect");
done();
});

namespace2.adapter.remoteDisconnect(socket1.id, false);
});

describe("socketsJoin", () => {
it("makes all socket instances join the specified room", (done) => {
namespace1.socketsJoin("room1");
Expand Down

0 comments on commit fb760d9

Please sign in to comment.