Skip to content

Commit d9bcb19

Browse files
feat: add support for Socket.IO v3
Related blog post: https://socket.io/blog/socket-io-3-release/ See also: socketio/socket.io-adapter@1.1.2...2.0.3 BREAKING CHANGES: - all the requests (for inter-node communication) now return a Promise instead of accepting a callback Before: ```js io.of('/').adapter.allRooms((err, rooms) => { console.log(rooms); // an array containing all rooms (accross every node) }); ``` After: ```js const rooms = await io.of('/').adapter.allRooms(); console.log(rooms); // a Set containing all rooms (across every node) ``` - RedisAdapter.clients() is renamed to RedisAdapter.sockets() See socketio/socket.io-adapter@130f28a - RedisAdapter.customHook() and RedisAdapter.customRequest() are removed Those methods will be replaced by a more intuitive API in a future iteration. - support for Node.js 8 is dropped See https://github.com/nodejs/Release
1 parent 2e39996 commit d9bcb19

File tree

9 files changed

+2137
-1222
lines changed

9 files changed

+2137
-1222
lines changed

Diff for: .gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,6 @@ results
1313

1414
npm-debug.log
1515
node_modules
16-
.idea
16+
.idea
17+
.nyc_output/
18+
dist/

Diff for: .travis.yml

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
language: node_js
22
sudo: false
33
node_js:
4-
- "8"
54
- "10"
65
- "12"
7-
- "13"
6+
- "14"
87
notifications:
98
irc: "irc.freenode.org#socket.io"
109
services:

Diff for: README.md

+62-62
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,27 @@
33
[![Build Status](https://travis-ci.org/socketio/socket.io-redis.svg?branch=master)](https://travis-ci.org/socketio/socket.io-redis)
44
[![NPM version](https://badge.fury.io/js/socket.io-redis.svg)](http://badge.fury.io/js/socket.io-redis)
55

6+
## Table of contents
7+
8+
- [How to use](#how-to-use)
9+
- [Compatibility table](#compatibility-table)
10+
- [API](#api)
11+
- [adapter(uri[, opts])](#adapteruri-opts)
12+
- [adapter(opts)](#adapteropts)
13+
- [RedisAdapter](#redisadapter)
14+
- [RedisAdapter#sockets(rooms: Set<String>)](#redisadaptersocketsrooms-setstring)
15+
- [RedisAdapter#allRooms()](#redisadapterallrooms)
16+
- [RedisAdapter#remoteJoin(id:String, room:String)](#redisadapterremotejoinidstring-roomstring)
17+
- [RedisAdapter#remoteLeave(id:String, room:String)](#redisadapterremoteleaveidstring-roomstring)
18+
- [RedisAdapter#remoteDisconnect(id:String, close:Boolean)](#redisadapterremotedisconnectidstring-closeboolean)
19+
- [Client error handling](#client-error-handling)
20+
- [Custom client (eg: with authentication)](#custom-client-eg-with-authentication)
21+
- [With ioredis client](#with-ioredishttpsgithubcomluinioredis-client)
22+
- [Cluster example](#cluster-example)
23+
- [Sentinel Example](#sentinel-example)
24+
- [Protocol](#protocol)
25+
- [License](#license)
26+
627
## How to use
728

829
```js
@@ -32,6 +53,14 @@ will properly be broadcast to the clients through the Redis Pub/Sub mechanism.
3253
If you need to emit events to socket.io instances from a non-socket.io
3354
process, you should use [socket.io-emitter](https://github.com/socketio/socket.io-emitter).
3455

56+
## Compatibility table
57+
58+
| Redis Adapter version | Socket.IO server version |
59+
|-----------------------| ------------------------ |
60+
| 4.x | 1.x |
61+
| 5.x | 2.x |
62+
| 6.x | 3.x |
63+
3564
## API
3665

3766
### adapter(uri[, opts])
@@ -65,94 +94,65 @@ that a regular `Adapter` does not
6594
- `subClient`
6695
- `requestsTimeout`
6796

68-
### RedisAdapter#clients(rooms:Array, fn:Function)
97+
### RedisAdapter#sockets(rooms: Set<String>)
6998

70-
Returns the list of client IDs connected to `rooms` across all nodes. See [Namespace#clients(fn:Function)](https://github.com/socketio/socket.io#namespaceclientsfnfunction)
99+
Returns the list of socket IDs connected to `rooms` across all nodes. See [Namespace#allSockets()](https://socket.io/docs/v3/server-api/#namespace-allSockets)
71100

72101
```js
73-
io.of('/').adapter.clients((err, clients) => {
74-
console.log(clients); // an array containing all connected socket ids
75-
});
102+
const sockets = await io.of('/').adapter.sockets();
103+
console.log(sockets); // a Set containing all the connected socket ids
76104

77-
io.of('/').adapter.clients(['room1', 'room2'], (err, clients) => {
78-
console.log(clients); // an array containing socket ids in 'room1' and/or 'room2'
79-
});
80-
81-
// you can also use
105+
const sockets = await io.of('/').adapter.sockets(new Set(['room1', 'room2']));
106+
console.log(sockets); // a Set containing the socket ids in 'room1' or in 'room2'
82107

83-
io.in('room3').clients((err, clients) => {
84-
console.log(clients); // an array containing socket ids in 'room3'
85-
});
86-
```
87-
88-
### RedisAdapter#clientRooms(id:String, fn:Function)
89-
90-
Returns the list of rooms the client with the given ID has joined (even on another node).
91-
92-
```js
93-
io.of('/').adapter.clientRooms('<my-id>', (err, rooms) => {
94-
if (err) { /* unknown id */ }
95-
console.log(rooms); // an array containing every room a given id has joined.
96-
});
108+
// this method is also exposed by the Server instance
109+
const sockets = io.in('room3').allSockets();
110+
console.log(sockets); // a Set containing the socket ids in 'room3'
97111
```
98112

99-
### RedisAdapter#allRooms(fn:Function)
113+
### RedisAdapter#allRooms()
100114

101115
Returns the list of all rooms.
102116

103117
```js
104-
io.of('/').adapter.allRooms((err, rooms) => {
105-
console.log(rooms); // an array containing all rooms (accross every node)
106-
});
107-
```
108-
109-
### RedisAdapter#remoteJoin(id:String, room:String, fn:Function)
110-
111-
Makes the socket with the given id join the room. The callback will be called once the socket has joined the room, or with an `err` argument if the socket was not found.
112-
113-
```js
114-
io.of('/').adapter.remoteJoin('<my-id>', 'room1', (err) => {
115-
if (err) { /* unknown id */ }
116-
// success
117-
});
118+
const rooms = await io.of('/').adapter.allRooms();
119+
console.log(rooms); // a Set containing all rooms (across every node)
118120
```
119121

120-
### RedisAdapter#remoteLeave(id:String, room:String, fn:Function)
122+
### RedisAdapter#remoteJoin(id:String, room:String)
121123

122-
Makes the socket with the given id leave the room. The callback will be called once the socket has left the room, or with an `err` argument if the socket was not found.
124+
Makes the socket with the given id join the room.
123125

124126
```js
125-
io.of('/').adapter.remoteLeave('<my-id>', 'room1', (err) => {
126-
if (err) { /* unknown id */ }
127-
// success
128-
});
127+
try {
128+
await io.of('/').adapter.remoteJoin('<my-id>', 'room1');
129+
} catch (e) {
130+
// the socket was not found
131+
}
129132
```
130133

131-
### RedisAdapter#remoteDisconnect(id:String, close:Boolean, fn:Function)
134+
### RedisAdapter#remoteLeave(id:String, room:String)
132135

133-
Makes the socket with the given id to get disconnected. If `close` is set to true, it also closes the underlying socket. The callback will be called once the socket was disconnected, or with an `err` argument if the socket was not found.
136+
Makes the socket with the given id leave the room.
134137

135138
```js
136-
io.of('/').adapter.remoteDisconnect('<my-id>', true, (err) => {
137-
if (err) { /* unknown id */ }
138-
// success
139-
});
139+
try {
140+
await io.of('/').adapter.remoteLeave('<my-id>', 'room1');
141+
} catch (e) {
142+
// the socket was not found
143+
}
140144
```
141145

142-
### RedisAdapter#customRequest(data:Object, fn:Function)
146+
### RedisAdapter#remoteDisconnect(id:String, close:Boolean)
143147

144-
Sends a request to every nodes, that will respond through the `customHook` method.
148+
Makes the socket with the given id to get disconnected. If `close` is set to true, it also closes the underlying socket.
145149

146150
```js
147-
// on every node
148-
io.of('/').adapter.customHook = (data, cb) => {
149-
cb('hello ' + data);
151+
try {
152+
await io.of('/').adapter.remoteDisconnect('<my-id>', true);
153+
} catch (e) {
154+
// the socket was not found
150155
}
151-
152-
// then
153-
io.of('/').adapter.customRequest('john', function(err, replies){
154-
console.log(replies); // an array ['hello john', ...] with one element per node
155-
});
156156
```
157157

158158
## Client error handling
@@ -190,7 +190,7 @@ const sub = redis.createClient(port, host, { auth_pass: "pwd" });
190190
io.adapter(redisAdapter({ pubClient: pub, subClient: sub }));
191191
```
192192

193-
## With [ioredis](https://github.com/luin/ioredis) client
193+
## With ioredis client
194194

195195
### Cluster example
196196

0 commit comments

Comments
 (0)