nestjs-stan-transport
TypeScript icon, indicating that this package has built-in type declarations

1.0.3 • Public • Published

✨ NATS Streaming Server and Client transport modules for NestJS

Library that implements NATS Streaming server(subscriber) and client(publisher) transport using NestJS and his Microservices library.

⭐️ Features

  • ClientStan: A class that implements Nest's ClientProxy to emit event to NATS Streaming.
  • ServerStan: A class that implements Nest's CustomTransportStrategy to listen to events from NATS Streaming.

📖 Contents

🛠 Installation

npm install nestjs-stan-transport --save

Usage

Importing

For publishing:

app.module.ts

import { Module } from '@nestjs/common';
import { StanTransportModule } from 'nestjs-stan-transport';
 
@Module({
  imports: [
    StanTransportModule.forRoot({
            url: 'nats://localhost:4222',
            group: 'user.workers',
            clusterId: 'test-cluster',
            clientId: 'user-service-publisher',
        })
  ],
})
export class ApplicationModule {}

For custom transport strategy(subscriber) main.ts

 
 
import {NestFactory} from '@nestjs/core';
import {ServerStan} from "nestjs-stan-transport";
import {AppModule} from './app.module';
 
async function bootstrap() {
 
    const options = {
        strategy: new ServerStan({
            url: 'nats://localhost:4222',
            group: 'user.workers',
            clusterId: 'test-cluster',
            clientId: 'user-service',
        })
    }
 
    const app = await NestFactory.createMicroservice(AppModule, options)
    await app.listen(() => logger.log('Microservice is listening'));
}
 
bootstrap();
 

For Cluster connection provide a comma-separated URL string:

const options = {
        strategy: new ServerStan({
            url: 'nats://server1:4222,nats://server2:4222',
            group: 'user.workers',
            clusterId: 'test-cluster',
            clientId: 'user-service',
        })
    }

Event emitter

For emit events to NATS Streaming inject the ClientStan instance:

import { ClientStan } from "nestjs-stan-transport";
 
export class UserPublisherService  {
 
    constructor(
        private readonly client: ClientStan,
    ) {}
 
    async execute(event: UserCreatedEventModel) {
        this.client.emit('UserCreatedEventSubject', event);
    }
 
}

Event Listener

Use @EventPattern annotation if you want to listen a specific event.

import { EventPattern } from '@nestjs/microservices';
 
export class UserHanlderService  {
 
   
    @EventPattern('UserCreatedEventSubject')
    async handleEvent(event: UserCreatedDto) {
        this.client.emit('UserCreatedEventSubject', event);
    }
 
}

📝 Stay in touch

Package Sidebar

Install

npm i nestjs-stan-transport

Weekly Downloads

6

Version

1.0.3

License

MIT

Unpacked Size

34.1 kB

Total Files

37

Last publish

Collaborators

  • victorhugo