Skip to content

NATS Streaming Server and Client transport modules for NestJS

License

Notifications You must be signed in to change notification settings

hugoviktor/nestjs-stan-transport

Repository files navigation

✨ 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

About

NATS Streaming Server and Client transport modules for NestJS

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

Packages

No packages published