Skip to content

Commit a70db12

Browse files
feat: include features from Socket.IO v4
The codebase is now written in TypeScript. Additional features: - https://socket.io/docs/v4/migrating-from-3-x-to-4-0/#Allow-excluding-specific-rooms-when-broadcasting - https://socket.io/docs/v4/migrating-from-3-x-to-4-0/#Additional-utility-methods - https://socket.io/docs/v4/migrating-from-3-x-to-4-0/#Typed-events BREAKING CHANGE: the "redis" package is not installed by default anymore, you'll now need to create your own redis client and pass it to the Emitter constructor Before: ```js const io = require("socket.io-emitter")({ host: "127.0.0.1", port: 6379 }); ``` After: ```js const { Emitter } = require("socket.io-emitter"); const { createClient } = require("redis"); const redisClient = createClient(); const io = new Emitter(redisClient); ``` Related: - #89 - #90 - #95
1 parent ad920e4 commit a70db12

File tree

12 files changed

+2828
-926
lines changed

12 files changed

+2828
-926
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
node_modules
2+
dist/
3+
.nyc_output/

Readme.md

Lines changed: 112 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -23,130 +23,170 @@ It must be used in conjunction with [socket.io-redis](https://github.com/socketi
2323
The current version is compatible with both:
2424

2525
- `socket.io-redis@5` (`socket.io@2`)
26-
- `socket.io-redis@6` (`socket.io@3`)
26+
- `socket.io-redis@6` (`socket.io@3` & `socket.io@4`)
2727

2828
## Table of content
2929

3030
- [How to use](#how-to-use)
31-
- [Examples](#examples)
32-
- [Error handling](#error-handling)
31+
- [CommonJS](#commonjs)
32+
- [TypeScript](#typescript)
33+
- [Emit cheatsheet](#emit-cheatsheet)
3334
- [API](#api)
3435
- [Emitter(client[, opts])](#emitterclient-opts)
35-
- [Emitter(clientUri[, opts])](#emitterclienturi-opts)
36-
- [Emitter(opts)](#emitteropts)
37-
- [Emitter#to(room:String):Emitter](#emittertoroomstringemitter)
38-
- [Emitter#in(room:String):Emitter](#emitterinroomstringemitter)
39-
- [Emitter#of(namespace:String):Emitter](#emitterofnamespacestringemitter)
36+
- [Emitter#to(room)](#emittertoroomstringbroadcastoperator)
37+
- [Emitter#in(room)](#emitterinroomstringbroadcastoperator)
38+
- [Emitter#except(room)](#emitterexceptroomstringbroadcastoperator)
39+
- [Emitter#of(namespace)](#emitterofnamespacestringemitter)
40+
- [Emitter#socketsJoin()](#emittersocketsjoinroomsstringstring)
41+
- [Emitter#socketsLeave()](#emittersocketsleaveroomsstringstring)
42+
- [Emitter#disconnectSockets()](#emitterdisconnectsocketscloseboolean)
4043
- [License](#license)
4144

4245
## How to use
4346

47+
### CommonJS
48+
49+
Installation: `npm i socket.io-emitter redis`
50+
4451
```js
45-
const io = require('socket.io-emitter')({ host: '127.0.0.1', port: 6379 });
52+
const { Emitter } = require("socket.io-emitter");
53+
const { createClient } = require("redis"); // not included, needs to be explicitly installed
54+
55+
const redisClient = createClient();
56+
const io = new Emitter(redisClient);
57+
4658
setInterval(() => {
47-
io.emit('time', new Date);
59+
io.emit("time", new Date);
4860
}, 5000);
4961
```
50-
```js
51-
// Different constructor options.
52-
53-
//1. Initialize with host:port string
54-
const io = require('socket.io-emitter')("localhost:6379")
55-
// 2. Initlize with host, port object.
56-
const io = require('socket.io-emitter')({ host: '127.0.0.1', port: 6379 });
57-
// 3. Can use other node_redis compatible client eg; ioredis.
58-
59-
const Redis = require("ioredis");
60-
const redis = new Redis();
61-
const io = require('socket.io-emitter')(redis);
62-
63-
// Make the emitter works with redis clustered environment.
64-
const Cluster = new Redis.Cluster([
65-
{
66-
host: "localhost",
67-
port: 6379
68-
},
69-
{
70-
host: "localhost",
71-
port: 6378
72-
},
73-
]);
74-
const io = require('socket.io-emitter')(Cluster);
7562

63+
### TypeScript
64+
65+
Installation: `npm i socket.io-emitter redis @types/redis`
66+
67+
```ts
68+
import { Emitter } from "socket.io-emitter";
69+
import { createClient } from "redis";
70+
71+
const redisClient = createClient();
72+
const io = new Emitter(redisClient);
73+
74+
setInterval(() => {
75+
io.emit("time", new Date);
76+
}, 5000);
77+
```
78+
79+
With typed events:
80+
81+
```ts
82+
import { Emitter } from ".";
83+
import { createClient } from "redis";
84+
85+
interface Events {
86+
basicEmit: (a: number, b: string, c: number[]) => void;
87+
}
88+
89+
const redisClient = createClient();
90+
const io = new Emitter<Events>(redisClient);
91+
92+
io.emit("basicEmit", 1, "2", [3]);
7693
```
7794

78-
## Examples
95+
## Emit cheatsheet
7996

8097
```js
81-
const io = require('socket.io-emitter')({ host: '127.0.0.1', port: 6379 });
98+
const { Emitter } = require("socket.io-emitter");
99+
const { createClient } = require("redis"); // not included, needs to be explicitly installed
100+
101+
const redisClient = createClient();
102+
const io = new Emitter(redisClient);
82103

83104
// sending to all clients
84-
io.emit('broadcast', /* ... */);
105+
io.emit(/* ... */);
85106

86-
// sending to all clients in 'game' room
87-
io.to('game').emit('new-game', /* ... */);
107+
// sending to all clients in 'room1' room
108+
io.to("room1").emit(/* ... */);
109+
110+
// sending to all clients in 'room1' except those in 'room2'
111+
io.to("room1").except("room2").emit(/* ... */);
88112

89113
// sending to individual socketid (private message)
90-
io.to(socketId).emit('private', /* ... */);
114+
io.to(socketId).emit(/* ... */);
91115

92-
const nsp = io.of('/admin');
116+
const nsp = io.of("/admin");
93117

94118
// sending to all clients in 'admin' namespace
95-
nsp.emit('namespace', /* ... */);
119+
nsp.emit(/* ... */);
96120

97121
// sending to all clients in 'admin' namespace and in 'notifications' room
98-
nsp.to('notifications').emit('namespace', /* ... */);
122+
nsp.to("notifications").emit(/* ... */);
99123
```
100124

101125
**Note:** acknowledgements are not supported
102126

103-
## Error handling
104-
105-
Access the `redis` to subscribe to its `error` event:
106-
107-
```js
108-
const emitter = require('socket.io-emitter')("localhost:6379");
109-
110-
emitter.redis.on('error', (err) => {
111-
console.log(err);
112-
});
113-
```
114-
115127
## API
116128

117129
### Emitter(client[, opts])
118130

119131
`client` is a [node_redis](https://github.com/mranney/node_redis)
120132
compatible client that has been initialized with the `return_buffers`
121-
option set to `true`. This argument is optional.
133+
option set to `true`.
122134

123135
The following options are allowed:
124136

125137
- `key`: the name of the key to pub/sub events on as prefix (`socket.io`)
126-
- `host`: host to connect to redis on (`localhost`)
127-
- `port`: port to connect to redis on (`6379`)
128-
- `socket`: unix domain socket to connect to redis on (`"/tmp/redis.sock"`)
129138

130-
### Emitter(clientUri[, opts])
139+
### Emitter#to(room:String):BroadcastOperator
140+
### Emitter#in(room:String):BroadcastOperator
131141

132-
Same as above, but `clientUri` is a string of the format `host:port`
133-
to connect to redis to.
142+
Specifies a specific `room` that you want to emit to.
134143

135-
### Emitter(opts)
144+
### Emitter#except(room:String):BroadcastOperator
136145

137-
If you don't want to supply a redis client object, and want
138-
`socket.io-emitter` to intiialize one for you, make sure to supply the
139-
`host` and `port` options.
146+
Specifies a specific `room` that you want to exclude from broadcasting.
140147

141-
### Emitter#to(room:String):Emitter
142-
### Emitter#in(room:String):Emitter
148+
### Emitter#of(namespace:String):Emitter
143149

144-
Specifies a specific `room` that you want to emit to.
150+
Specifies a specific namespace that you want to emit to.
145151

152+
### Emitter#socketsJoin(rooms:String|String[])
146153

147-
### Emitter#of(namespace:String):Emitter
154+
Makes the matching socket instances join the specified rooms:
148155

149-
Specifies a specific namespace that you want to emit to.
156+
```js
157+
// make all Socket instances join the "room1" room
158+
io.socketsJoin("room1");
159+
160+
// make all Socket instances of the "admin" namespace in the "room1" room join the "room2" room
161+
io.of("/admin").in("room1").socketsJoin("room2");
162+
```
163+
164+
### Emitter#socketsLeave(rooms:String|String[])
165+
166+
Makes the matching socket instances leave the specified rooms:
167+
168+
```js
169+
// make all Socket instances leave the "room1" room
170+
io.socketsLeave("room1");
171+
172+
// make all Socket instances of the "admin" namespace in the "room1" room leave the "room2" room
173+
io.of("/admin").in("room1").socketsLeave("room2");
174+
```
175+
176+
### Emitter#disconnectSockets(close:boolean)
177+
178+
Makes the matching socket instances disconnect:
179+
180+
```js
181+
// make all Socket instances disconnect
182+
io.disconnectSockets();
183+
184+
// make all Socket instances of the "admin" namespace in the "room1" room disconnect
185+
io.of("/admin").in("room1").disconnectSockets();
186+
187+
// this also works with a single socket ID
188+
io.of("/admin").in(theSocketId).disconnectSockets();
189+
```
150190

151191
## License
152192

docker-compose.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
version: '2.0'
2+
services:
3+
redis:
4+
image: redis:5
5+
ports:
6+
- "6379:6379"

0 commit comments

Comments
 (0)