8.3.0 • Published 1 year ago

@socket.io/redis-adapter v8.3.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

Socket.IO Redis adapter

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

Table of contents

Supported features

Featuresocket.io versionSupport
Socket management4.0.0:white_check_mark: YES (since version 6.1.0)
Inter-server communication4.1.0:white_check_mark: YES (since version 7.0.0)
Broadcast with acknowledgements4.5.0:white_check_mark: YES (since version 7.2.0)
Connection state recovery4.6.0:x: NO

Installation

npm install @socket.io/redis-adapter

Compatibility table

Redis Adapter versionSocket.IO server version
4.x1.x
5.x2.x
6.0.x3.x
6.1.x4.x
7.x and above4.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

NameDescriptionDefault value
keyThe prefix for the Redis Pub/Sub channels.socket.io
requestsTimeoutAfter this timeout the adapter will stop waiting from responses to request.5_000
publishOnSpecificResponseChannelWhether to publish a response to the channel specific to the requesting node.false
parserThe parser to use for encoding and decoding messages sent to Redis.-

Sharded adapter

NameDescriptionDefault value
channelPrefixThe prefix for the Redis Pub/Sub channels.socket.io
subscriptionModeThe subscription mode impacts the number of Redis Pub/Sub channels used by the adapter.dynamic

License

MIT

@ckenx/kenx-socketio@fishbot/core@fortification/core@infinitebrahmanuniverse/nolb-_soc@everything-registry/sub-chunk-849symple@dal/dftr-api@cantilever-tech/lib-nodejs@canttech/lib-nodejs@tanphat199/lb-infraomnihivestrapi-plugin-pushertrucoshi@asapjs/socket@appolo/socket@appstack-io/pubsub@afrosintech/app@alicilin/node-mmq@awamwang/egg-socket.io@bedrock-foundation/server@azteam/express@afrosintech/tcp-server@afrosintech/mvc@afiether/gateware-kit@afiether/kit@betsys-nestjs/websocket-redis-io-adapter@bucai/egg-socket.io@budibase/server@decentverse/serveryekonga-server@fluidframework/server-services-shared@flip-b/bot@fjedi/rest-api@flecks/redis@fuseble.inc/node@lakutata-component/tunnel@lakutata-module/service@lindorm-io/node-server@lindorm/pylon@mthang1801/domain-driven-design@mthang1801/library@nexgin/totaljsapplication@nivinjoseph/n-sock@nodearch/socket.io-redis@medyll/idae-socket@minimaltech/node-infra@ready.io/server@oridune/socket@paralect/hive@squidcloud/coremy-base-nest-web3ongopatient-apipayment-microservicepayment-microservice-clientprovider-apinode-mayanestjs-libniro-healthservices-audyxsocket.io-brokersocket-templatesigma-coresofa-proahmed22hexabotlinebridgemasonite-broadcast-servermercurius-chat@tspvivek/baasix@synced/game-server@synced/scheduler@synced/game-api@synced/game-dispatcher@toolshub/shared@topgroup/diginext@vulkano/core@vostro/c2-engine@yqg/node@xyndata/oa3-api-service@yutai/egg-socket.ioappbeeapi_node_cpaapi_oddcloudcms-serverdecentversedmdeck-simulatoreensyegg-socket.io-newegg-jianghuers-httpferdigfloor-managerfaceoff-backendgamemilanmodel@servicelabsco/nestjs-utility-services
8.3.0

1 year ago

8.2.1

2 years ago

8.2.0

2 years ago

8.1.0

2 years ago

8.0.1

2 years ago

8.0.0

3 years ago

7.2.0

3 years ago

7.1.0

4 years ago

7.0.1

4 years ago

7.0.0

4 years ago