0.0.5 • Published 9 months ago

@crudmates/redis-pubsub v0.0.5

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

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!☕️

0.0.3

9 months ago

0.0.5

9 months ago

0.0.4

9 months ago

0.0.2

10 months ago

0.0.1

10 months ago