Skip to content

Configuration

Options

PubSubPlugin accepts IPubSubPluginOptions:

OptionTypeDefaultDescription
clientstring'default'Named Redis client used for publishing. The dedicated subscriber connection is cloned from this client's config.
channelPrefixstring''Prefix applied to every channel and pattern. Empty by default so channels interoperate with non-RedisX publishers/subscribers. Handlers always see the logical (unprefixed) names.

Synchronous Setup

typescript
import { Module } from '@nestjs/common';
import { RedisModule } from '@nestjs-redisx/core';
import { PubSubPlugin } from '@nestjs-redisx/pubsub';

@Module({
  imports: [
    RedisModule.forRoot({
      clients: {
        host: 'localhost',
        port: 6379,
      },
      plugins: [
        new PubSubPlugin({
          channelPrefix: 'app:', // optional namespace for all channels
        }),
      ],
    }),
  ],
})
export class AppModule {}

The dedicated subscriber connection

A Redis connection in subscriber mode cannot execute regular commands. The plugin therefore creates a second client named <client>:pubsub-subscriber with the same connection config and driver type as your named client, and routes all SUBSCRIBE/PSUBSCRIBE traffic through it. Your main client keeps serving cache/locks/regular commands untouched. The connection is registered with the client manager and closed with the application.

Driver support

DriverSingleClusterSentinel
ioredis
node-redis

With node-redis, Pub/Sub uses the v4 listener API and is supported on single-node connections; use ioredis for Pub/Sub over Cluster/Sentinel topologies.

Released under the MIT License.