@socket.io/redis-adapter
Advanced tools
Comparing version 7.2.0 to 8.0.0
@@ -1,2 +0,6 @@ | ||
import { Adapter, BroadcastOptions, Room, SocketId } from "socket.io-adapter"; | ||
import { Adapter, BroadcastOptions, Room } from "socket.io-adapter"; | ||
interface Parser { | ||
decode: (msg: any) => any; | ||
encode: (msg: any) => any; | ||
} | ||
export interface RedisAdapterOptions { | ||
@@ -25,2 +29,7 @@ /** | ||
publishOnSpecificResponseChannel: boolean; | ||
/** | ||
* The parser to use for encoding and decoding messages sent to Redis. | ||
* This option defaults to using `notepack.io`, a MessagePack implementation. | ||
*/ | ||
parser: Parser; | ||
} | ||
@@ -43,2 +52,3 @@ /** | ||
readonly publishOnSpecificResponseChannel: boolean; | ||
readonly parser: Parser; | ||
private readonly channel; | ||
@@ -97,10 +107,2 @@ private readonly requestChannel; | ||
/** | ||
* @deprecated Please use `namespace.fetchSockets()` instead. | ||
* | ||
* Gets a list of sockets by sid. | ||
* | ||
* @param {Set<Room>} rooms the explicit set of rooms to check. | ||
*/ | ||
sockets(rooms: Set<Room>): Promise<Set<SocketId>>; | ||
/** | ||
* Gets the list of all rooms (across every node) | ||
@@ -111,33 +113,2 @@ * | ||
allRooms(): Promise<Set<Room>>; | ||
/** | ||
* @deprecated Please use `namespace.socketsJoin()` instead. | ||
* | ||
* Makes the socket with the given id join the room | ||
* | ||
* @param {String} id - socket id | ||
* @param {String} room - room name | ||
* @public | ||
*/ | ||
remoteJoin(id: SocketId, room: Room): Promise<void>; | ||
/** | ||
* @deprecated Please use `namespace.socketsLeave()` instead. | ||
* | ||
* Makes the socket with the given id leave the room | ||
* | ||
* @param {String} id - socket id | ||
* @param {String} room - room name | ||
* @public | ||
*/ | ||
remoteLeave(id: SocketId, room: Room): Promise<void>; | ||
/** | ||
* @deprecated Please use `namespace.disconnectSockets()` instead. | ||
* | ||
* Makes the socket with the given id to be forcefully disconnected | ||
* | ||
* @param {String} id - socket id | ||
* @param {Boolean} close - if `true`, closes the underlying connection | ||
* | ||
* @public | ||
*/ | ||
remoteDisconnect(id: SocketId, close?: boolean): Promise<void>; | ||
fetchSockets(opts: BroadcastOptions): Promise<any[]>; | ||
@@ -157,1 +128,2 @@ addSockets(opts: BroadcastOptions, rooms: Room[]): void; | ||
} | ||
export {}; |
@@ -72,2 +72,3 @@ "use strict"; | ||
this.publishOnSpecificResponseChannel = !!opts.publishOnSpecificResponseChannel; | ||
this.parser = opts.parser || msgpack; | ||
const prefix = opts.key || "socket.io"; | ||
@@ -122,3 +123,3 @@ this.channel = prefix + "#" + nsp.name + "#"; | ||
} | ||
const args = msgpack.decode(msg); | ||
const args = this.parser.decode(msg); | ||
const [uid, packet, opts] = args; | ||
@@ -162,3 +163,3 @@ if (this.uid === uid) | ||
else { | ||
request = msgpack.decode(msg); | ||
request = this.parser.decode(msg); | ||
} | ||
@@ -317,3 +318,3 @@ } | ||
debug("received acknowledgement with value %j", arg); | ||
this.publishResponse(request, msgpack.encode({ | ||
this.publishResponse(request, this.parser.encode({ | ||
type: RequestType.BROADCAST_ACK, | ||
@@ -356,3 +357,3 @@ requestId: request.requestId, | ||
else { | ||
response = msgpack.decode(msg); | ||
response = this.parser.decode(msg); | ||
} | ||
@@ -462,3 +463,3 @@ } | ||
}; | ||
const msg = msgpack.encode([this.uid, packet, rawOpts]); | ||
const msg = this.parser.encode([this.uid, packet, rawOpts]); | ||
let channel = this.channel; | ||
@@ -484,3 +485,3 @@ if (opts.rooms && opts.rooms.size === 1) { | ||
}; | ||
const request = msgpack.encode({ | ||
const request = this.parser.encode({ | ||
uid: this.uid, | ||
@@ -506,41 +507,2 @@ requestId, | ||
/** | ||
* @deprecated Please use `namespace.fetchSockets()` instead. | ||
* | ||
* Gets a list of sockets by sid. | ||
* | ||
* @param {Set<Room>} rooms the explicit set of rooms to check. | ||
*/ | ||
async sockets(rooms) { | ||
const localSockets = await super.sockets(rooms); | ||
const numSub = await this.getNumSub(); | ||
debug('waiting for %d responses to "sockets" request', numSub); | ||
if (numSub <= 1) { | ||
return Promise.resolve(localSockets); | ||
} | ||
const requestId = uid2(6); | ||
const request = JSON.stringify({ | ||
uid: this.uid, | ||
requestId, | ||
type: RequestType.SOCKETS, | ||
rooms: [...rooms], | ||
}); | ||
return new Promise((resolve, reject) => { | ||
const timeout = setTimeout(() => { | ||
if (this.requests.has(requestId)) { | ||
reject(new Error("timeout reached while waiting for sockets response")); | ||
this.requests.delete(requestId); | ||
} | ||
}, this.requestsTimeout); | ||
this.requests.set(requestId, { | ||
type: RequestType.SOCKETS, | ||
numSub, | ||
resolve, | ||
timeout, | ||
msgCount: 1, | ||
sockets: localSockets, | ||
}); | ||
this.pubClient.publish(this.requestChannel, request); | ||
}); | ||
} | ||
/** | ||
* Gets the list of all rooms (across every node) | ||
@@ -581,117 +543,2 @@ * | ||
} | ||
/** | ||
* @deprecated Please use `namespace.socketsJoin()` instead. | ||
* | ||
* Makes the socket with the given id join the room | ||
* | ||
* @param {String} id - socket id | ||
* @param {String} room - room name | ||
* @public | ||
*/ | ||
remoteJoin(id, room) { | ||
const requestId = uid2(6); | ||
const socket = this.nsp.sockets.get(id); | ||
if (socket) { | ||
socket.join(room); | ||
return Promise.resolve(); | ||
} | ||
const request = JSON.stringify({ | ||
uid: this.uid, | ||
requestId, | ||
type: RequestType.REMOTE_JOIN, | ||
sid: id, | ||
room, | ||
}); | ||
return new Promise((resolve, reject) => { | ||
const timeout = setTimeout(() => { | ||
if (this.requests.has(requestId)) { | ||
reject(new Error("timeout reached while waiting for remoteJoin response")); | ||
this.requests.delete(requestId); | ||
} | ||
}, this.requestsTimeout); | ||
this.requests.set(requestId, { | ||
type: RequestType.REMOTE_JOIN, | ||
resolve, | ||
timeout, | ||
}); | ||
this.pubClient.publish(this.requestChannel, request); | ||
}); | ||
} | ||
/** | ||
* @deprecated Please use `namespace.socketsLeave()` instead. | ||
* | ||
* Makes the socket with the given id leave the room | ||
* | ||
* @param {String} id - socket id | ||
* @param {String} room - room name | ||
* @public | ||
*/ | ||
remoteLeave(id, room) { | ||
const requestId = uid2(6); | ||
const socket = this.nsp.sockets.get(id); | ||
if (socket) { | ||
socket.leave(room); | ||
return Promise.resolve(); | ||
} | ||
const request = JSON.stringify({ | ||
uid: this.uid, | ||
requestId, | ||
type: RequestType.REMOTE_LEAVE, | ||
sid: id, | ||
room, | ||
}); | ||
return new Promise((resolve, reject) => { | ||
const timeout = setTimeout(() => { | ||
if (this.requests.has(requestId)) { | ||
reject(new Error("timeout reached while waiting for remoteLeave response")); | ||
this.requests.delete(requestId); | ||
} | ||
}, this.requestsTimeout); | ||
this.requests.set(requestId, { | ||
type: RequestType.REMOTE_LEAVE, | ||
resolve, | ||
timeout, | ||
}); | ||
this.pubClient.publish(this.requestChannel, request); | ||
}); | ||
} | ||
/** | ||
* @deprecated Please use `namespace.disconnectSockets()` instead. | ||
* | ||
* Makes the socket with the given id to be forcefully disconnected | ||
* | ||
* @param {String} id - socket id | ||
* @param {Boolean} close - if `true`, closes the underlying connection | ||
* | ||
* @public | ||
*/ | ||
remoteDisconnect(id, close) { | ||
const requestId = uid2(6); | ||
const socket = this.nsp.sockets.get(id); | ||
if (socket) { | ||
socket.disconnect(close); | ||
return Promise.resolve(); | ||
} | ||
const request = JSON.stringify({ | ||
uid: this.uid, | ||
requestId, | ||
type: RequestType.REMOTE_DISCONNECT, | ||
sid: id, | ||
close, | ||
}); | ||
return new Promise((resolve, reject) => { | ||
const timeout = setTimeout(() => { | ||
if (this.requests.has(requestId)) { | ||
reject(new Error("timeout reached while waiting for remoteDisconnect response")); | ||
this.requests.delete(requestId); | ||
} | ||
}, this.requestsTimeout); | ||
this.requests.set(requestId, { | ||
type: RequestType.REMOTE_DISCONNECT, | ||
resolve, | ||
timeout, | ||
}); | ||
this.pubClient.publish(this.requestChannel, request); | ||
}); | ||
} | ||
async fetchSockets(opts) { | ||
@@ -698,0 +545,0 @@ var _a; |
{ | ||
"name": "@socket.io/redis-adapter", | ||
"version": "7.2.0", | ||
"version": "8.0.0", | ||
"description": "The Socket.IO Redis adapter, allowing to broadcast events between several Socket.IO servers", | ||
@@ -27,5 +27,5 @@ "license": "MIT", | ||
"debug": "~4.3.1", | ||
"notepack.io": "~2.2.0", | ||
"notepack.io": "~3.0.1", | ||
"socket.io-adapter": "^2.4.0", | ||
"uid2": "0.0.3" | ||
"uid2": "1.0.0" | ||
}, | ||
@@ -38,3 +38,3 @@ "devDependencies": { | ||
"ioredis": "^4.0.0", | ||
"mocha": "^3.4.2", | ||
"mocha": "^10.1.0", | ||
"nyc": "^15.1.0", | ||
@@ -41,0 +41,0 @@ "prettier": "^2.1.2", |
@@ -15,6 +15,5 @@ # socket.io-redis | ||
- [API](#api) | ||
- [adapter(uri[, opts])](#adapteruri-opts) | ||
- [adapter(opts)](#adapteropts) | ||
- [adapter(pubClient, subClient[, opts])](#adapterpubclient-subclient-opts) | ||
- [RedisAdapter](#redisadapter) | ||
- [RedisAdapter#sockets(rooms: Set<String>)](#redisadaptersocketsrooms-setstring) | ||
- [RedisAdapter#sockets(rooms: Set<String>)](#redisadaptersocketsrooms-setstring) | ||
- [RedisAdapter#allRooms()](#redisadapterallrooms) | ||
@@ -24,5 +23,3 @@ - [RedisAdapter#remoteJoin(id:String, room:String)](#redisadapterremotejoinidstring-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) | ||
- [With ioredis client](#with-ioredis-client) | ||
- [Cluster example](#cluster-example) | ||
@@ -202,2 +199,3 @@ - [Sentinel Example](#sentinel-example) | ||
- `requestsTimeout`: optional, after this timeout the adapter will stop waiting from responses to request (`5000ms`) | ||
- `parser`: optional, parser to use for encoding and decoding messages passed through Redis ([`notepack.io`](https://www.npmjs.com/package/notepack.io)) | ||
@@ -214,4 +212,5 @@ ### RedisAdapter | ||
- `requestsTimeout` | ||
- `parser` | ||
### RedisAdapter#sockets(rooms: Set<String>) | ||
### RedisAdapter#sockets(rooms: Set<String>) | ||
@@ -218,0 +217,0 @@ Returns the list of socket IDs connected to `rooms` across all nodes. See [Namespace#allSockets()](https://socket.io/docs/v3/server-api/#namespace-allSockets) |
45964
842
373
+ Addednotepack.io@3.0.1(transitive)
+ Addeduid2@1.0.0(transitive)
- Removednotepack.io@2.2.0(transitive)
- Removeduid2@0.0.3(transitive)
Updatednotepack.io@~3.0.1
Updateduid2@1.0.0