Skip to content

Commit

Permalink
feat: add support for Socket.IO v3
Browse files Browse the repository at this point in the history
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
  • Loading branch information
darrachequesne committed Nov 12, 2020
1 parent 2e39996 commit d9bcb19
Show file tree
Hide file tree
Showing 9 changed files with 2,137 additions and 1,222 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ results

npm-debug.log
node_modules
.idea
.idea
.nyc_output/
dist/
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
language: node_js
sudo: false
node_js:
- "8"
- "10"
- "12"
- "13"
- "14"
notifications:
irc: "irc.freenode.org#socket.io"
services:
Expand Down
124 changes: 62 additions & 62 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,27 @@
[![Build Status](https://travis-ci.org/socketio/socket.io-redis.svg?branch=master)](https://travis-ci.org/socketio/socket.io-redis)
[![NPM version](https://badge.fury.io/js/socket.io-redis.svg)](http://badge.fury.io/js/socket.io-redis)

## Table of contents

- [How to use](#how-to-use)
- [Compatibility table](#compatibility-table)
- [API](#api)
- [adapter(uri[, opts])](#adapteruri-opts)
- [adapter(opts)](#adapteropts)
- [RedisAdapter](#redisadapter)
- [RedisAdapter#sockets(rooms: Set<String>)](#redisadaptersocketsrooms-setstring)
- [RedisAdapter#allRooms()](#redisadapterallrooms)
- [RedisAdapter#remoteJoin(id:String, room:String)](#redisadapterremotejoinidstring-roomstring)
- [RedisAdapter#remoteLeave(id:String, room:String)](#redisadapterremoteleaveidstring-roomstring)
- [RedisAdapter#remoteDisconnect(id:String, close:Boolean)](#redisadapterremotedisconnectidstring-closeboolean)
- [Client error handling](#client-error-handling)
- [Custom client (eg: with authentication)](#custom-client-eg-with-authentication)
- [With ioredis client](#with-ioredishttpsgithubcomluinioredis-client)
- [Cluster example](#cluster-example)
- [Sentinel Example](#sentinel-example)
- [Protocol](#protocol)
- [License](#license)

## How to use

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

## Compatibility table

| Redis Adapter version | Socket.IO server version |
|-----------------------| ------------------------ |
| 4.x | 1.x |
| 5.x | 2.x |
| 6.x | 3.x |

## API

### adapter(uri[, opts])
Expand Down Expand Up @@ -65,94 +94,65 @@ that a regular `Adapter` does not
- `subClient`
- `requestsTimeout`

### RedisAdapter#clients(rooms:Array, fn:Function)
### RedisAdapter#sockets(rooms: Set<String>)

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

```js
io.of('/').adapter.clients((err, clients) => {
console.log(clients); // an array containing all connected socket ids
});
const sockets = await io.of('/').adapter.sockets();
console.log(sockets); // a Set containing all the connected socket ids

io.of('/').adapter.clients(['room1', 'room2'], (err, clients) => {
console.log(clients); // an array containing socket ids in 'room1' and/or 'room2'
});

// you can also use
const sockets = await io.of('/').adapter.sockets(new Set(['room1', 'room2']));
console.log(sockets); // a Set containing the socket ids in 'room1' or in 'room2'

io.in('room3').clients((err, clients) => {
console.log(clients); // an array containing socket ids in 'room3'
});
```

### RedisAdapter#clientRooms(id:String, fn:Function)

Returns the list of rooms the client with the given ID has joined (even on another node).

```js
io.of('/').adapter.clientRooms('<my-id>', (err, rooms) => {
if (err) { /* unknown id */ }
console.log(rooms); // an array containing every room a given id has joined.
});
// this method is also exposed by the Server instance
const sockets = io.in('room3').allSockets();
console.log(sockets); // a Set containing the socket ids in 'room3'
```

### RedisAdapter#allRooms(fn:Function)
### RedisAdapter#allRooms()

Returns the list of all rooms.

```js
io.of('/').adapter.allRooms((err, rooms) => {
console.log(rooms); // an array containing all rooms (accross every node)
});
```

### RedisAdapter#remoteJoin(id:String, room:String, fn:Function)

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.

```js
io.of('/').adapter.remoteJoin('<my-id>', 'room1', (err) => {
if (err) { /* unknown id */ }
// success
});
const rooms = await io.of('/').adapter.allRooms();
console.log(rooms); // a Set containing all rooms (across every node)
```

### RedisAdapter#remoteLeave(id:String, room:String, fn:Function)
### RedisAdapter#remoteJoin(id:String, room:String)

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.
Makes the socket with the given id join the room.

```js
io.of('/').adapter.remoteLeave('<my-id>', 'room1', (err) => {
if (err) { /* unknown id */ }
// success
});
try {
await io.of('/').adapter.remoteJoin('<my-id>', 'room1');
} catch (e) {
// the socket was not found
}
```

### RedisAdapter#remoteDisconnect(id:String, close:Boolean, fn:Function)
### RedisAdapter#remoteLeave(id:String, room:String)

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.
Makes the socket with the given id leave the room.

```js
io.of('/').adapter.remoteDisconnect('<my-id>', true, (err) => {
if (err) { /* unknown id */ }
// success
});
try {
await io.of('/').adapter.remoteLeave('<my-id>', 'room1');
} catch (e) {
// the socket was not found
}
```

### RedisAdapter#customRequest(data:Object, fn:Function)
### RedisAdapter#remoteDisconnect(id:String, close:Boolean)

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

```js
// on every node
io.of('/').adapter.customHook = (data, cb) => {
cb('hello ' + data);
try {
await io.of('/').adapter.remoteDisconnect('<my-id>', true);
} catch (e) {
// the socket was not found
}

// then
io.of('/').adapter.customRequest('john', function(err, replies){
console.log(replies); // an array ['hello john', ...] with one element per node
});
```

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

## With [ioredis](https://github.com/luin/ioredis) client
## With ioredis client

### Cluster example

Expand Down
Loading

0 comments on commit d9bcb19

Please sign in to comment.