Skip to content

Commit 5feabda

Browse files
feat: implement the serverSideEmit functionality
Syntax: ```js emitter.serverSideEmit("hello", "world"); // Socket.IO server io.on("hello", (arg) => { console.log(arg); // prints "world" }); ```
1 parent fc12a3c commit 5feabda

File tree

5 files changed

+97
-79
lines changed

5 files changed

+97
-79
lines changed

Readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ The emitter is also available in other programming languages:
1717
- Perl: https://metacpan.org/pod/SocketIO::Emitter
1818
- Rust: https://github.com/epli2/socketio-rust-emitter
1919

20-
It must be used in conjunction with [socket.io-redis](https://github.com/socketio/socket.io-redis/).
20+
It must be used in conjunction with [`@socket.io/redis-adapter`](https://github.com/socketio/socket.io-redis-adapter/).
2121

2222
The current version is compatible with both:
2323

lib/index.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ enum RequestType {
2424
REMOTE_LEAVE = 3,
2525
REMOTE_DISCONNECT = 4,
2626
REMOTE_FETCH = 5,
27+
SERVER_SIDE_EMIT = 6,
2728
}
2829

2930
export interface EmitterOptions {
@@ -201,6 +202,27 @@ export class Emitter<EmitEvents extends EventsMap = DefaultEventsMap> {
201202
this.broadcastOptions
202203
).disconnectSockets(close);
203204
}
205+
206+
/**
207+
* Send a packet to the Socket.IO servers in the cluster
208+
*
209+
* @param args - any number of serializable arguments
210+
*/
211+
public serverSideEmit(...args: any[]): void {
212+
const withAck = typeof args[args.length - 1] === "function";
213+
214+
if (withAck) {
215+
throw new Error("Acknowledgements are not supported");
216+
}
217+
218+
const request = JSON.stringify({
219+
uid: UID,
220+
type: RequestType.SERVER_SIDE_EMIT,
221+
data: args,
222+
});
223+
224+
this.redisClient.publish(this.broadcastOptions.requestChannel, request);
225+
}
204226
}
205227

206228
export const RESERVED_EVENTS: ReadonlySet<string | Symbol> = new Set(<const>[

package-lock.json

Lines changed: 56 additions & 70 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"socket.io-parser": "~4.0.4"
2525
},
2626
"devDependencies": {
27+
"@socket.io/redis-adapter": "^7.0.0",
2728
"@types/mocha": "^8.2.1",
2829
"@types/node": "^14.14.35",
2930
"@types/redis": "^2.8.28",
@@ -32,9 +33,8 @@
3233
"nyc": "^15.1.0",
3334
"prettier": "^2.2.1",
3435
"redis": "^3.0.2",
35-
"socket.io": "^4.0.0",
36-
"socket.io-client": "^4.0.0",
37-
"socket.io-redis": "^6.1.0",
36+
"socket.io": "^4.1.1",
37+
"socket.io-client": "^4.1.1",
3838
"ts-node": "^9.1.1",
3939
"typescript": "^4.2.3"
4040
}

test/index.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import expect = require("expect.js");
22
import { createClient, RedisClient } from "redis";
33
import { Server, Socket } from "socket.io";
44
import { io as ioc, Socket as ClientSocket } from "socket.io-client";
5-
import { createAdapter } from "socket.io-redis";
5+
import { createAdapter } from "@socket.io/redis-adapter";
66
import { createServer } from "http";
77
import { Emitter } from "..";
88
import type { AddressInfo } from "net";
@@ -44,10 +44,7 @@ describe("emitter", () => {
4444
subClient = createClient();
4545

4646
io = new Server(httpServer, {
47-
adapter: createAdapter({
48-
pubClient,
49-
subClient,
50-
}),
47+
adapter: createAdapter(pubClient, subClient),
5148
});
5249

5350
httpServer.listen(() => {
@@ -274,5 +271,18 @@ describe("emitter", () => {
274271
clientSockets[2].on("disconnect", partialDone);
275272
});
276273
});
274+
275+
describe("serverSideEmit", () => {
276+
it("sends an event to other server instances", (done) => {
277+
emitter.serverSideEmit("hello", "world", 1, "2");
278+
279+
io.on("hello", (arg1, arg2, arg3) => {
280+
expect(arg1).to.eql("world");
281+
expect(arg2).to.eql(1);
282+
expect(arg3).to.eql("2");
283+
done();
284+
});
285+
});
286+
});
277287
});
278288
});

0 commit comments

Comments
 (0)