Skip to content

Commit 1c3e471

Browse files
feat: add emitter based on Redis streams
Related: socketio/socket.io-redis-streams-adapter#8
1 parent 693080c commit 1c3e471

File tree

12 files changed

+1355
-66
lines changed

12 files changed

+1355
-66
lines changed

package-lock.json

Lines changed: 77 additions & 65 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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"packages/socket.io-parser",
1313
"packages/socket.io-client",
1414
"packages/socket.io",
15-
"packages/socket.io-postgres-emitter"
15+
"packages/socket.io-postgres-emitter",
16+
"packages/socket.io-redis-streams-emitter"
1617
],
1718
"overrides": {
1819
"@types/estree": "0.0.52",
@@ -32,6 +33,7 @@
3233
"@rollup/plugin-node-resolve": "^15.2.3",
3334
"@sinonjs/fake-timers": "^11.2.2",
3435
"@socket.io/postgres-adapter": "^0.1.0",
36+
"@socket.io/redis-streams-adapter": "~0.2.2",
3537
"@types/debug": "^4.1.12",
3638
"@types/expect.js": "^0.3.32",
3739
"@types/mocha": "^10.0.7",
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright (c) 2025-present Guillermo Rauch and Socket.IO contributors
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
# Socket.IO Redis Streams emitter
2+
3+
The `@socket.io/redis-streams-emitter` package allows you to easily communicate with a group of Socket.IO servers from another Node.js process (server-side).
4+
5+
It must be used in conjunction with [`@socket.io/redis-streams-adapter`](https://github.com/socketio/socket.io-redis-streams-adapter).
6+
7+
**Table of contents**
8+
9+
<!-- TOC -->
10+
* [Installation](#installation)
11+
* [Usage](#usage)
12+
* [With the `redis` package](#with-the-redis-package)
13+
* [With the `redis` package and a Redis cluster](#with-the-redis-package-and-a-redis-cluster)
14+
* [With the `ioredis` package](#with-the-ioredis-package)
15+
* [With the `ioredis` package and a Redis cluster](#with-the-ioredis-package-and-a-redis-cluster)
16+
* [Options](#options)
17+
* [API](#api)
18+
* [`Emitter(redisClient[, nsp][, opts])`](#emitterredisclient-nsp-opts)
19+
* [`Emitter#to(room:string):BroadcastOperator`](#emittertoroomstringbroadcastoperator)
20+
* [`Emitter#in(room:string):BroadcastOperator`](#emitterinroomstringbroadcastoperator)
21+
* [`Emitter#except(room:string):BroadcastOperator`](#emitterexceptroomstringbroadcastoperator)
22+
* [`Emitter#of(namespace:string):Emitter`](#emitterofnamespacestringemitter)
23+
* [`Emitter#socketsJoin(rooms:string|string[])`](#emittersocketsjoinroomsstringstring)
24+
* [`Emitter#socketsLeave(rooms:string|string[])`](#emittersocketsleaveroomsstringstring)
25+
* [`Emitter#disconnectSockets(close:boolean)`](#emitterdisconnectsocketscloseboolean)
26+
* [`Emitter#serverSideEmit(ev:string[,...args:any[]])`](#emitterserversideemitevstringargsany)
27+
* [License](#license)
28+
<!-- TOC -->
29+
30+
## Installation
31+
32+
```
33+
npm install @socket.io/redis-streams-emitter redis
34+
```
35+
36+
## Usage
37+
38+
### With the `redis` package
39+
40+
```js
41+
import { createClient } from "redis";
42+
import { Emitter } from "@socket.io/redis-streams-emitter";
43+
44+
const redisClient = createClient({
45+
url: "redis://localhost:6379"
46+
});
47+
48+
await redisClient.connect();
49+
50+
const io = new Emitter(redisClient);
51+
52+
setInterval(() => {
53+
io.emit("ping", new Date());
54+
}, 1000);
55+
```
56+
57+
### With the `redis` package and a Redis cluster
58+
59+
```js
60+
import { createCluster } from "redis";
61+
import { Emitter } from "@socket.io/redis-streams-emitter";
62+
63+
const redisClient = createCluster({
64+
rootNodes: [
65+
{
66+
url: "redis://localhost:7000",
67+
},
68+
{
69+
url: "redis://localhost:7001",
70+
},
71+
{
72+
url: "redis://localhost:7002",
73+
},
74+
],
75+
});
76+
77+
await redisClient.connect();
78+
79+
const io = new Emitter(redisClient);
80+
81+
setInterval(() => {
82+
io.emit("ping", new Date());
83+
}, 1000);
84+
```
85+
86+
### With the `ioredis` package
87+
88+
```js
89+
import { Redis } from "ioredis";
90+
import { Emitter } from "@socket.io/redis-streams-emitter";
91+
92+
const redisClient = new Redis();
93+
94+
const io = new Emitter(redisClient);
95+
96+
setInterval(() => {
97+
io.emit("ping", new Date());
98+
}, 1000);
99+
```
100+
101+
### With the `ioredis` package and a Redis cluster
102+
103+
```js
104+
import { Cluster } from "ioredis";
105+
import { Emitter } from "@socket.io/redis-streams-emitter";
106+
107+
const redisClient = new Cluster([
108+
{
109+
host: "localhost",
110+
port: 7000,
111+
},
112+
{
113+
host: "localhost",
114+
port: 7001,
115+
},
116+
{
117+
host: "localhost",
118+
port: 7002,
119+
},
120+
]);
121+
122+
const io = new Emitter(redisClient);
123+
124+
setInterval(() => {
125+
io.emit("ping", new Date());
126+
}, 1000);
127+
```
128+
129+
## Options
130+
131+
| Name | Description | Default value |
132+
|--------------|--------------------------------------------------------------------|---------------|
133+
| `streamName` | The name of the Redis stream. | `socket.io` |
134+
| `maxLen` | The maximum size of the stream. Almost exact trimming (~) is used. | `10_000` |
135+
136+
## API
137+
138+
### `Emitter(redisClient[, nsp][, opts])`
139+
140+
```js
141+
const io = new Emitter(redisClient);
142+
```
143+
144+
### `Emitter#to(room:string):BroadcastOperator`
145+
### `Emitter#in(room:string):BroadcastOperator`
146+
147+
Specifies a specific `room` that you want to emit to.
148+
149+
```js
150+
io.to("room1").emit("hello");
151+
```
152+
153+
### `Emitter#except(room:string):BroadcastOperator`
154+
155+
Specifies a specific `room` that you want to exclude from broadcasting.
156+
157+
```js
158+
io.except("room2").emit("hello");
159+
```
160+
161+
### `Emitter#of(namespace:string):Emitter`
162+
163+
Specifies a specific namespace that you want to emit to.
164+
165+
```js
166+
const customNamespace = io.of("/custom");
167+
168+
customNamespace.emit("hello");
169+
```
170+
171+
### `Emitter#socketsJoin(rooms:string|string[])`
172+
173+
Makes the matching socket instances join the specified rooms:
174+
175+
```js
176+
// make all Socket instances join the "room1" room
177+
io.socketsJoin("room1");
178+
179+
// make all Socket instances of the "admin" namespace in the "room1" room join the "room2" room
180+
io.of("/admin").in("room1").socketsJoin("room2");
181+
```
182+
183+
### `Emitter#socketsLeave(rooms:string|string[])`
184+
185+
Makes the matching socket instances leave the specified rooms:
186+
187+
```js
188+
// make all Socket instances leave the "room1" room
189+
io.socketsLeave("room1");
190+
191+
// make all Socket instances of the "admin" namespace in the "room1" room leave the "room2" room
192+
io.of("/admin").in("room1").socketsLeave("room2");
193+
```
194+
195+
### `Emitter#disconnectSockets(close:boolean)`
196+
197+
Makes the matching socket instances disconnect:
198+
199+
```js
200+
// make all Socket instances disconnect
201+
io.disconnectSockets();
202+
203+
// make all Socket instances of the "admin" namespace in the "room1" room disconnect
204+
io.of("/admin").in("room1").disconnectSockets();
205+
206+
// this also works with a single socket ID
207+
io.of("/admin").in(theSocketId).disconnectSockets();
208+
```
209+
210+
### `Emitter#serverSideEmit(ev:string[,...args:any[]])`
211+
212+
Emits an event that will be received by each Socket.IO server of the cluster.
213+
214+
```js
215+
io.serverSideEmit("ping");
216+
```
217+
218+
## License
219+
220+
[MIT](./LICENSE)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
services:
2+
redis:
3+
image: redis:5
4+
ports:
5+
- "6379:6379"
6+
7+
redis-cluster:
8+
image: grokzen/redis-cluster:7.0.10
9+
ports:
10+
- "7000-7005:7000-7005"
11+
12+
valkey:
13+
image: valkey/valkey:8
14+
ports:
15+
- "6389:6379"

0 commit comments

Comments
 (0)