@socket.io/redis-adapter
Advanced tools
Changelog
8.2.0 (2023-05-02)
Sharded Pub/Sub was introduced in Redis 7.0 in order to help scaling the usage of Pub/Sub in cluster mode.
Reference: https://redis.io/docs/manual/pubsub/#sharded-pubsub
A dedicated adapter can be created with the createShardedAdapter()
method:
import { Server } from 'socket.io';
import { createClient } from 'redis';
import { createShardedAdapter } from '@socket.io/redis-adapter';
const pubClient = createClient({ host: 'localhost', port: 6379 });
const subClient = pubClient.duplicate();
await Promise.all([
pubClient.connect(),
subClient.connect()
]);
const io = new Server({
adapter: createShardedAdapter(pubClient, subClient)
});
io.listen(3000);
Minimum requirements:
redis@4.6.0
Added in e70b1bd.
The redis
package now supports Redis cluster.
Added in 77ef42c.
The subscriptionMode
option allows to configure how many Redis Pub/Sub channels are used:
Useful when used with dynamic namespaces.
The default value, useful when some rooms have a low number of clients (so only a few Socket.IO servers are notified).
const io = new Server({
adapter: createShardedAdapter(pubClient, subClient, {
subscriptionMode: "static"
})
});
Added in d3388bf.
Huge thanks to @winchell for helping!
Changelog
8.1.0 (2023-02-08)
The socket.io-adapter
package was added to the list of peerDependencies
, in order to fix sync issues with the version imported by the socket.io
package (see f07ff7b).
The close()
method was implemented, in order to be used with the new cleanupEmptyChildNamespaces
option.
Reference: https://github.com/socketio/socket.io/releases/tag/4.6.0
Added in fe89f7e.
Changelog
8.0.0 (2022-12-07)
Example with msgpackr:
import { unpack, pack } from "msgpackr";
io.adapter(createAdapter(pubClient, subClient, {
parser: {
encode(val) {
return pack(val);
},
decode(val) {
return unpack(val);
}
}
}));
Related: https://github.com/socketio/socket.io/commit/b25495c069031674da08e19aed68922c7c7a0e28
Reference: https://github.com/msgpack/msgpack/blob/master/spec.md#timestamp-extension-type
Previous versions of the adapter will not be able to parse the Date objects sent by newer versions.
Changelog
7.0.0 (2021-05-11)
@socket.io/redis-adapter
(3cac178)Before:
io.adapter(redisAdapter({ host: "localhost", port: 6379 }));
After:
const pubClient = createClient({ host: "localhost", port: 6379 });
const subClient = pubClient.duplicate();
io.adapter(redisAdapter(pubClient, subClient));