MCPcopy
hub / github.com/socketio/socket.io-redis-adapter

github.com/socketio/socket.io-redis-adapter @8.3.0 sqlite

repository ↗ · DeepWiki ↗ · release 8.3.0 ↗
63 symbols 148 edges 8 files 8 documented · 13%
README

Socket.IO Redis adapter

The @socket.io/redis-adapter package allows broadcasting packets between multiple Socket.IO servers.

Diagram of Socket.IO packets forwarded through Redis

Table of contents

Supported features

Feature socket.io version Support
Socket management 4.0.0 :white_check_mark: YES (since version 6.1.0)
Inter-server communication 4.1.0 :white_check_mark: YES (since version 7.0.0)
Broadcast with acknowledgements 4.5.0 :white_check_mark: YES (since version 7.2.0)
Connection state recovery 4.6.0 :x: NO

Installation

npm install @socket.io/redis-adapter

Compatibility table

Redis Adapter version Socket.IO server version
4.x 1.x
5.x 2.x
6.0.x 3.x
6.1.x 4.x
7.x and above 4.3.1 and above

Usage

With the redis package

import { createClient } from "redis";
import { Server } from "socket.io";
import { createAdapter } from "@socket.io/redis-adapter";

const pubClient = createClient({ url: "redis://localhost:6379" });
const subClient = pubClient.duplicate();

await Promise.all([
  pubClient.connect(),
  subClient.connect()
]);

const io = new Server({
  adapter: createAdapter(pubClient, subClient)
});

io.listen(3000);

With the redis package and a Redis cluster

import { createCluster } from "redis";
import { Server } from "socket.io";
import { createAdapter } from "@socket.io/redis-adapter";

const pubClient = createCluster({
  rootNodes: [
    {
      url: "redis://localhost:7000",
    },
    {
      url: "redis://localhost:7001",
    },
    {
      url: "redis://localhost:7002",
    },
  ],
});
const subClient = pubClient.duplicate();

await Promise.all([
  pubClient.connect(),
  subClient.connect()
]);

const io = new Server({
  adapter: createAdapter(pubClient, subClient)
});

io.listen(3000);

With the ioredis package

import { Redis } from "ioredis";
import { Server } from "socket.io";
import { createAdapter } from "@socket.io/redis-adapter";

const pubClient = new Redis();
const subClient = pubClient.duplicate();

const io = new Server({
  adapter: createAdapter(pubClient, subClient)
});

io.listen(3000);

With the ioredis package and a Redis cluster

import { Cluster } from "ioredis";
import { Server } from "socket.io";
import { createAdapter } from "@socket.io/redis-adapter";

const pubClient = new Cluster([
  {
    host: "localhost",
    port: 7000,
  },
  {
    host: "localhost",
    port: 7001,
  },
  {
    host: "localhost",
    port: 7002,
  },
]);
const subClient = pubClient.duplicate();

const io = new Server({
  adapter: createAdapter(pubClient, subClient)
});

io.listen(3000);

With Redis sharded Pub/Sub

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/interact/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:

Note: it is not currently possible to use the sharded adapter with the ioredis package and a Redis cluster (reference).

Options

Default adapter

Name Description Default value
key The prefix for the Redis Pub/Sub channels. socket.io
requestsTimeout After this timeout the adapter will stop waiting from responses to request. 5_000
publishOnSpecificResponseChannel Whether to publish a response to the channel specific to the requesting node. false
parser The parser to use for encoding and decoding messages sent to Redis. -

Sharded adapter

Name Description Default value
channelPrefix The prefix for the Redis Pub/Sub channels. socket.io
subscriptionMode The subscription mode impacts the number of Redis Pub/Sub channels used by the adapter. dynamic

License

MIT

Extension points exported contracts — how you extend this code

Request (Interface)
(no doc)
lib/index.ts
ShardedRedisAdapterOptions (Interface)
(no doc)
lib/sharded-adapter.ts
TestContext (Interface)
(no doc)
test/util.ts
AckRequest (Interface)
(no doc)
lib/index.ts
Parser (Interface)
(no doc)
lib/index.ts
RedisAdapterOptions (Interface)
(no doc)
lib/index.ts

Core symbols most depended-on inside this repo

shouldNotHappen
called by 13
test/util.ts
testSuite
called by 12
test/specifics.ts
createAdapter
called by 8
lib/index.ts
publishResponse
called by 8
lib/index.ts
times
called by 8
test/util.ts
sleep
called by 8
test/util.ts
close
called by 6
lib/index.ts
createShardedAdapter
called by 6
lib/sharded-adapter.ts

Shape

Method 29
Function 23
Interface 6
Class 4
Enum 1

Languages

TypeScript100%

Modules by API surface

lib/index.ts27 symbols
lib/sharded-adapter.ts16 symbols
lib/util.ts8 symbols
test/util.ts5 symbols
test/specifics.ts3 symbols
test/custom-parser.ts2 symbols
test/test-runner.ts1 symbols
test/index.ts1 symbols

Dependencies from manifests, versioned

@types/expect.js0.3.29 · 1×
@types/mocha8.2.1 · 1×
@types/node14.14.7 · 1×
debug4.3.1 · 1×
expect.js0.3.1 · 1×
ioredis5.3.2 · 1×
mocha10.1.0 · 1×
notepack.io3.0.1 · 1×
nyc15.1.0 · 1×
prettier2.8.7 · 1×
redis4.6.6 · 1×
redis-v3

For agents

$ claude mcp add socket.io-redis-adapter \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact