Skip to content

Exclude specific sockets when emitting #3789

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions lib/namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ export class Namespace extends EventEmitter {
/** @private */
_rooms: Set<Room> = new Set();

/** @private */
_except: Set<Room> = new Set();

/** @private */
_flags: any = {};

Expand Down Expand Up @@ -123,6 +126,18 @@ export class Namespace extends EventEmitter {
return this;
}

/**
* Excludes a room when emitting.
*
* @param name
* @return self
* @public
*/
public except(name: Room): Namespace {
this._except.add(name);
return this;
}

/**
* Adds a new client.
*
Expand Down Expand Up @@ -203,14 +218,17 @@ export class Namespace extends EventEmitter {

const rooms = new Set(this._rooms);
const flags = Object.assign({}, this._flags);
const except = new Set(this._except);

// reset flags
this._rooms.clear();
this._flags = {};
this._except.clear();

this.adapter.broadcast(packet, {
rooms: rooms,
flags: flags,
except: except,
});

return true;
Expand Down
17 changes: 16 additions & 1 deletion lib/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export class Socket extends EventEmitter {
> = [];
private flags: BroadcastFlags = {};
private _rooms: Set<Room> = new Set();
private _except: Set<Room> = new Set();
private _anyListeners?: Array<(...args: any[]) => void>;

/**
Expand Down Expand Up @@ -165,14 +166,16 @@ export class Socket extends EventEmitter {

const rooms = new Set(this._rooms);
const flags = Object.assign({}, this.flags);
const except = new Set(this._except);

// reset flags
this._rooms.clear();
this.flags = {};
this._except.clear();

if (rooms.size || flags.broadcast) {
this.adapter.broadcast(packet, {
except: new Set([this.id]),
except: new Set([this.id, ...except]),
rooms: rooms,
flags: flags,
});
Expand Down Expand Up @@ -207,6 +210,18 @@ export class Socket extends EventEmitter {
return this;
}

/**
* Excludes a room when broadcasting.
*
* @param name
* @return self
* @public
*/
public except(name: Room): Socket {
this._except.add(name);
return this;
}

/**
* Sends a `message` event.
*
Expand Down
63 changes: 63 additions & 0 deletions test/socket.io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,35 @@ describe("socket.io", () => {
});
});

it("should exclude a specific socket when emitting", (done) => {
const srv = createServer();
const sio = new Server(srv);

const nsp = sio.of("/nsp");

srv.listen(() => {
const socket1 = client(srv, "/nsp");
const socket2 = client(srv, "/nsp");

socket2.on("a", () => {
done(new Error("not"));
});
socket1.on("a", () => {
done();
});

nsp.on("connection", (socket) => {
socket.on("id", (cb) => {
cb(socket.id);
});
});

socket2.emit("id", (id) => {
nsp.except(id).emit("a");
});
});
});

describe("dynamic namespaces", () => {
it("should allow connections to dynamic namespaces with a regex", (done) => {
const srv = createServer();
Expand Down Expand Up @@ -2195,6 +2224,40 @@ describe("socket.io", () => {
});
});
});

it("should exclude specific sockets when broadcasting", (done) => {
const srv = createServer();
const sio = new Server(srv);

srv.listen(() => {
const socket1 = client(srv, { multiplex: false });
const socket2 = client(srv, { multiplex: false });
const socket3 = client(srv, { multiplex: false });

socket2.on("a", () => {
done(new Error("not"));
});
socket3.on("a", () => {
done(new Error("not"));
});
socket1.on("a", () => {
done();
});

sio.on("connection", (socket) => {
socket.on("id", (cb) => {
cb(socket.id);
});
socket.on("exclude", (id) => {
socket.broadcast.except(id).emit("a");
});
});

socket2.emit("id", (id) => {
socket3.emit("exclude", id);
});
});
});
});

describe("middleware", () => {
Expand Down