nest-nats-management
TypeScript icon, indicating that this package has built-in type declarations

1.2.0 • Public • Published

nest-nats-management

Overview

This package provides a NestJS module for integrating with NATS Streaming (STAN), enabling publish-subscribe messaging in a microservices architecture. It offers automatic connection management, message publishing, and subscription handling with manual acknowledgment support.

Features

  • 📡 Seamless NATS Streaming Integration
  • 🔄 Automatic Reconnection Handling
  • Manual Acknowledgment Support
  • Efficient Pub/Sub Mechanism
  • 🚀 Easy to Use in Any NestJS Application

Installation

Install the package using npm:

npm install nest-nats-management

Usage

1️⃣ Importing the Module in AppModule

import { Module } from '@nestjs/common';
import { NatsManagementModule } from 'nest-nats-management';

@Module({
  imports: [NatsManagementModule.forRoot('test-cluster', 'client-123', 'nats://localhost:4222')],
})
export class AppModule {}

2️⃣ Publishing Messages

import { Injectable } from '@nestjs/common';
import { NatsManagementService } from 'nest-nats-management';

@Injectable()
export class PersonnesService {
  constructor(private readonly natsService: NatsManagementService) {}

  async create(person: CreatePersonnesDto) {
    //Your logic here

    //publishing message
    await this.natsService.publish('person:created', person);
  }

  
}

3️⃣ Subscribing to Messages

import { Controller } from '@nestjs/common';
import { NatsManagementService } from 'nest-nats-management';

@Controller('personnes')
export class PersonnesController {
  constructor(private readonly natsService: NatsManagementService) {
    this.subscribeToPersonCreated();
  }

  private subscribeToPersonCreated() {
    this.natsService.subscribe('person:created', (message) => {
      console.log('📩 Received event in controller: person.created', message);
      //message reatment here
    });
  }
}

4️⃣ Unsubscribing from a Subject

this.natsService.unsubscribe('person:created');

License

This project is licensed under the ISC License.

Readme

Keywords

Package Sidebar

Install

npm i nest-nats-management

Weekly Downloads

55

Version

1.2.0

License

ISC

Unpacked Size

129 kB

Total Files

27

Last publish

Collaborators

  • swalest