@crudmates/redis-pubsub
v0.0.9
Published
A Redis-based Pub/Sub module for NestJS applications.
Downloads
717
Maintainers
Readme
A Redis-based Pub/Sub module for NestJS applications.
Description
This package provides a Redis-based Pub/Sub implementation for NestJS applications. It includes both event publishing and subscribing capabilities using Redis streams.
Installation
npm install @crudmates/redis-pubsub
Usage
Import the module
import { Module } from '@nestjs/common';
import { RedisPubSubModule } from '@crudmates/redis-pubsub';
@Module({
imports: [
RedisPubSubModule.forRoot({
redisUrl: 'redis://localhost:6379',
streamKey: 'events',
}),
],
})
export class AppModule {}
Publish an event
import { Injectable } from '@nestjs/common';
import { RedisPubSub } from '@crudmates/redis-pubsub';
@Injectable()
export class PubsubPublisherService {
constructor(private readonly publisher: RedisPubSub) {}
async orderCreated() {
await this.publisher.emit('order.created', { id: 1 });
}
}
Subscribe to an event
import { Injectable, OnModuleInit } from '@nestjs/common';
import { RedisPubSub } from '@crudmates/redis-pubsub';
@Injectable()
export class PubsubSubscriberService implements OnModuleInit {
constructor(private readonly subscriber: RedisPubSub) {}
onModuleInit() {
this.subscriber.init('order-service');
this.subscriber.on('order.created', this.handleOrderCreated.bind(this));
}
async handleOrderCreated(event: string, data: any) {
// Handle the event
}
}
License
This package is licensed under the MIT license.
Support the Project
Love this package? Show your support by giving us a ⭐ on GitHub! Feeling extra generous? You can buy us coffee to keep us fueled for more coding adventures!☕️