|
| 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) |
0 commit comments