DEV Community

Cover image for NestJS RedisX v1.7.0: Introducing Typed Pub/Sub for Real-Time Messaging
Suren Krmoian
Suren Krmoian

Posted on

NestJS RedisX v1.7.0: Introducing Typed Pub/Sub for Real-Time Messaging

Unleashing the Power of NestJS RedisX v1.7.0

NestJS RedisX just got a major upgrade with the release of version 1.7.0, including a shiny new plugin: @nestjs-redisx/pubsub. This plugin brings typed Redis Pub/Sub capabilities right into your NestJS applications, perfect for real-time messaging across multiple instances.

The Pub/Sub Puzzle

Redis Pub/Sub is fantastic, but it comes with its quirks. If you've ever tried to subscribe on your main Redis client, you probably hit a wall. In subscriber mode, your connection stops executing regular commands. Frustrating, right? The typical fix? You end up juggling a second connection, managing your subscriptions manually, and crossing fingers that everything shuts down gracefully.

Simplifying with the Plugin

Enter the new plugin. It handles a dedicated subscriber connection (<client>:pubsub-subscriber) for you. This means your main client stays functional while multiple handlers share a single Redis subscription. It's released once you're done.

Getting Started

Here's how you can set it up:

Installation:

npm install @nestjs-redisx/core @nestjs-redisx/pubsub ioredis
Enter fullscreen mode Exit fullscreen mode

Registration:

RedisModule.forRoot({
  clients: { host: 'localhost', port: 6379 },
  plugins: [
    new PubSubPlugin({ channelPrefix: 'app:' }),
  ],
})
Enter fullscreen mode Exit fullscreen mode

Publishing Events:

@Injectable()
export class UserPublisher {
  constructor(
    @Inject(PUBSUB_SERVICE)
    private readonly pubsub: IPubSubService,
  ) {}

  async userCreated(user: UserCreatedEvent) {
    await this.pubsub.publish('user.created', user);
  }
}
Enter fullscreen mode Exit fullscreen mode

Subscribing to Events:

Handlers are auto-discovered and connected at startup:

@Injectable()
export class UserEventsHandler {
  @Subscribe('user.created')
  onUserCreated(message: IPubSubMessage<UserCreatedEvent>) {
    this.gateway.broadcast('user:new', message.data);
  }

  @Subscribe({ pattern: 'order.*' })
  onAnyOrderEvent(message: IPubSubMessage) {
    this.gateway.broadcast(message.channel, message.data);
  }
}
Enter fullscreen mode Exit fullscreen mode

What's New in v1.7.0

Added:

  • PubSub Plugin: A new package with auto-discovery via the @Subscribe decorator.
  • Core Improvements: Enhanced driver layer for both node-redis and ioredis.
  • Testing: In-memory driver now fully supports Pub/Sub operations.
  • Tracing Enhancements: Improved traceability of Redis commands with native spans.

Changed:

  • Tracing Strategy: Default sampling strategy now aligns with OpenTelemetry SDK conventions.

A Note on Pub/Sub

Pub/Sub is all about immediacy—it's fire-and-forget. Messages hit only the currently subscribed connections. No persistence, no replay, no acknowledgment. Perfect for things like WebSocket broadcasts and cache invalidation. Need guaranteed delivery? Check out the Streams plugin.

For detailed documentation, visit the NestJS RedisX Docs. For the full release notes, head over to GitHub.

Top comments (0)