|
3 | 3 | [](https://travis-ci.org/socketio/socket.io-redis)
|
4 | 4 | [](http://badge.fury.io/js/socket.io-redis)
|
5 | 5 |
|
| 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 | + |
6 | 27 | ## How to use
|
7 | 28 |
|
8 | 29 | ```js
|
@@ -32,6 +53,14 @@ will properly be broadcast to the clients through the Redis Pub/Sub mechanism.
|
32 | 53 | If you need to emit events to socket.io instances from a non-socket.io
|
33 | 54 | process, you should use [socket.io-emitter](https://github.com/socketio/socket.io-emitter).
|
34 | 55 |
|
| 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 | + |
35 | 64 | ## API
|
36 | 65 |
|
37 | 66 | ### adapter(uri[, opts])
|
@@ -65,94 +94,65 @@ that a regular `Adapter` does not
|
65 | 94 | - `subClient`
|
66 | 95 | - `requestsTimeout`
|
67 | 96 |
|
68 |
| -### RedisAdapter#clients(rooms:Array, fn:Function) |
| 97 | +### RedisAdapter#sockets(rooms: Set<String>) |
69 | 98 |
|
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) |
71 | 100 |
|
72 | 101 | ```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 |
76 | 104 |
|
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' |
82 | 107 |
|
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' |
97 | 111 | ```
|
98 | 112 |
|
99 |
| -### RedisAdapter#allRooms(fn:Function) |
| 113 | +### RedisAdapter#allRooms() |
100 | 114 |
|
101 | 115 | Returns the list of all rooms.
|
102 | 116 |
|
103 | 117 | ```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) |
118 | 120 | ```
|
119 | 121 |
|
120 |
| -### RedisAdapter#remoteLeave(id:String, room:String, fn:Function) |
| 122 | +### RedisAdapter#remoteJoin(id:String, room:String) |
121 | 123 |
|
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. |
123 | 125 |
|
124 | 126 | ```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 | +} |
129 | 132 | ```
|
130 | 133 |
|
131 |
| -### RedisAdapter#remoteDisconnect(id:String, close:Boolean, fn:Function) |
| 134 | +### RedisAdapter#remoteLeave(id:String, room:String) |
132 | 135 |
|
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. |
134 | 137 |
|
135 | 138 | ```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 | +} |
140 | 144 | ```
|
141 | 145 |
|
142 |
| -### RedisAdapter#customRequest(data:Object, fn:Function) |
| 146 | +### RedisAdapter#remoteDisconnect(id:String, close:Boolean) |
143 | 147 |
|
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. |
145 | 149 |
|
146 | 150 | ```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 |
150 | 155 | }
|
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 |
| -}); |
156 | 156 | ```
|
157 | 157 |
|
158 | 158 | ## Client error handling
|
@@ -190,7 +190,7 @@ const sub = redis.createClient(port, host, { auth_pass: "pwd" });
|
190 | 190 | io.adapter(redisAdapter({ pubClient: pub, subClient: sub }));
|
191 | 191 | ```
|
192 | 192 |
|
193 |
| -## With [ioredis](https://github.com/luin/ioredis) client |
| 193 | +## With ioredis client |
194 | 194 |
|
195 | 195 | ### Cluster example
|
196 | 196 |
|
|
0 commit comments