### Installing Project Dependencies Source: https://github.com/redningsselskapet/nestjs-plugins/blob/master/packages/nats-listener/README.md Installs all required project dependencies listed in the package.json file using the npm package manager. ```bash $ npm install ``` -------------------------------- ### Installing Dependencies - NestJS Source: https://github.com/redningsselskapet/nestjs-plugins/blob/master/packages/nats-publisher/README.md Installs the project dependencies listed in the package.json file using npm. ```bash npm install ``` -------------------------------- ### Running NestJS App in Production Mode Source: https://github.com/redningsselskapet/nestjs-plugins/blob/master/packages/nats-listener/README.md Starts the NestJS application optimized for a production environment. ```bash $ npm run start:prod ``` -------------------------------- ### Running Application - NestJS Development Source: https://github.com/redningsselskapet/nestjs-plugins/blob/master/packages/nats-publisher/README.md Starts the NestJS application in development mode. ```bash npm run start ``` -------------------------------- ### Running Application - NestJS Production Mode Source: https://github.com/redningsselskapet/nestjs-plugins/blob/master/packages/nats-publisher/README.md Starts the NestJS application in production mode. ```bash npm run start:prod ``` -------------------------------- ### Running NestJS App in Development Mode Source: https://github.com/redningsselskapet/nestjs-plugins/blob/master/packages/nats-listener/README.md Starts the NestJS application in standard development mode, typically without watch functionality. ```bash $ npm run start ``` -------------------------------- ### Install NestJS Nats JetStream Transport Module Source: https://github.com/redningsselskapet/nestjs-plugins/blob/master/packages/nestjs-nats-jetstream-transport/README.md Installs the necessary npm packages for using the NestJS Nats JetStream transport module, including the core microservices package, the nats client, and the transport module itself. ```bash npm i @nestjs/microservices npm i nats npm i @nestjs-plugins/nestjs-nats-jetstream-transport ``` -------------------------------- ### Starting NestJS Application Listener - TypeScript Source: https://github.com/redningsselskapet/nestjs-plugins/blob/master/packages/nestjs-nats-jetstream-transport/README.md This snippet demonstrates how to start a NestJS application instance, making it listen for incoming connections on a specified port (3000 in this case). It typically follows the application bootstrapping process where the application module is created and configured. ```TypeScript app.listen(3000); } bootstrap(); ``` -------------------------------- ### Install Nats CLI on MacOS Source: https://github.com/redningsselskapet/nestjs-plugins/blob/master/packages/nestjs-nats-jetstream-transport/README.md Installs the Nats command-line interface tool on macOS using Homebrew, allowing interaction with Nats servers. ```bash brew install nats-io/nats-tools/nats ``` -------------------------------- ### Running NestJS App in Watch Mode Source: https://github.com/redningsselskapet/nestjs-plugins/blob/master/packages/nats-listener/README.md Starts the NestJS application in watch mode, which automatically recompiles and restarts the server upon detecting file changes. ```bash $ npm run start:dev ``` -------------------------------- ### Running Application - NestJS Watch Mode Source: https://github.com/redningsselskapet/nestjs-plugins/blob/master/packages/nats-publisher/README.md Starts the NestJS application in watch mode, automatically restarting on file changes. ```bash npm run start:dev ``` -------------------------------- ### Run Nats JetStream Server with Docker Source: https://github.com/redningsselskapet/nestjs-plugins/blob/master/packages/nestjs-nats-jetstream-transport/README.md Starts a Nats server instance with JetStream enabled in a Docker container, exposing the client (4222), clustering (6222), and monitoring (8222) ports. Runs in detached mode. ```bash docker run -d --name nats-main -p 4222:4222 -p 6222:6222 -p 8222:8222 nats -js -m 8222 ``` -------------------------------- ### Generating Test Coverage Report Source: https://github.com/redningsselskapet/nestjs-plugins/blob/master/packages/nats-listener/README.md Runs the project tests and generates a code coverage report, showing which parts of the code are exercised by tests. ```bash $ npm run test:cov ``` -------------------------------- ### Running Unit Tests Source: https://github.com/redningsselskapet/nestjs-plugins/blob/master/packages/nats-listener/README.md Executes the unit tests defined for the project using the configured test runner. ```bash $ npm run test ``` -------------------------------- ### Running End-to-End Tests Source: https://github.com/redningsselskapet/nestjs-plugins/blob/master/packages/nats-listener/README.md Executes the end-to-end (e2e) tests for the project, typically testing application flows from a user's perspective. ```bash $ npm run test:e2e ``` -------------------------------- ### Running Tests - NestJS Unit Tests Source: https://github.com/redningsselskapet/nestjs-plugins/blob/master/packages/nats-publisher/README.md Executes the unit tests for the NestJS application. ```bash npm run test ``` -------------------------------- ### Running Tests - NestJS Test Coverage Source: https://github.com/redningsselskapet/nestjs-plugins/blob/master/packages/nats-publisher/README.md Runs tests and generates a test coverage report for the NestJS application. ```bash npm run test:cov ``` -------------------------------- ### Running Tests - NestJS E2E Tests Source: https://github.com/redningsselskapet/nestjs-plugins/blob/master/packages/nats-publisher/README.md Executes the end-to-end tests for the NestJS application. ```bash npm run test:e2e ``` -------------------------------- ### Bootstrapping NestJS Hybrid Application with NATS JetStream Server Source: https://github.com/redningsselskapet/nestjs-plugins/blob/master/packages/nestjs-nats-jetstream-transport/README.md Sets up a NestJS application as a hybrid web server and microservice listener, configuring the NatsJetStreamServer with connection, consumer, and stream options to receive messages from NATS JetStream. ```javascript // main.js import { NestFactory } from '@nestjs/core'; import { AppModule } from './app.module'; import { CustomStrategy } from '@nestjs/microservices'; import { NatsJetStreamServer } from '@nestjs-plugins/nestjs-nats-jetstream-transport'; async function bootstrap() { const options: CustomStrategy = { strategy: new NatsJetStreamServer({ connectionOptions: { servers: 'localhost:4222', name: 'myservice-listener', }, consumerOptions: { deliverGroup: 'myservice-group', durable: 'myservice-durable', deliverTo: 'myservice-messages', manualAck: true, }, streamConfig: { name: 'mystream', subjects: ['order.*'], }, // streamConfig: [{ // name: 'mystream', // subjects: ['order.*'], // },{ // name: 'myOtherStream', // subjects: ['other.*'], // }], }), }; // hybrid microservice and web application const app = await NestFactory.create(AppModule); const microService = app.connectMicroservice(options); microService.listen(); } ``` -------------------------------- ### Add Nats JetStream Stream via CLI Source: https://github.com/redningsselskapet/nestjs-plugins/blob/master/packages/nestjs-nats-jetstream-transport/README.md Initiates the interactive process via the Nats CLI to add a new stream to the Nats JetStream server. Requires user input for stream name, subjects, and other configurations. ```bash nats stream add ``` -------------------------------- ### Implementing NATS JetStream Client in NestJS Service Source: https://github.com/redningsselskapet/nestjs-plugins/blob/master/packages/nestjs-nats-jetstream-transport/README.md Defines a NestJS service that uses NatsJetStreamClientProxy to emit events (order creation, update, deletion) and send requests (sum calculation) to NATS JetStream subjects/patterns. ```typescript // app.service.ts import { NatsJetStreamClientProxy } from '@nestjs-plugins/nestjs-nats-jetstream-transport'; import { Injectable } from '@nestjs/common'; import { PubAck } from 'nats'; import { Observable } from 'rxjs'; interface OrderCreatedEvent { id: number; product: string; quantity: number; } interface OrderUpdatedEvent { id: number; quantity: number; } interface OrderDeleteEvent { id: number; } const ORDER_CREATED = 'order.created'; const ORDER_UPDATED = 'order.updated'; const ORDER_DELETED = 'order.deleted'; @Injectable() export class AppService { constructor(private client: NatsJetStreamClientProxy) {} createOrder(): string { this.client .emit(ORDER_CREATED, { id: 1, product: 'Socks', quantity: 1, }) .subscribe((pubAck) => { console.log(pubAck); }); return 'order created.'; } updateOrder(): string { this.client .emit(ORDER_UPDATED, { id: 1, quantity: 10 }) .subscribe(); return 'order updated'; } deleteOrder(): string { this.client .emit(ORDER_DELETED, { id: 1 }) .subscribe((pubAck) => { console.log(pubAck); }); return 'order deleted'; } // request - response accumulate(payload: number[]): Observable { const pattern = { cmd: 'sum' }; return this.client.send(pattern, payload); } } ``` -------------------------------- ### Configuring NATS JetStream Transport Module in NestJS Source: https://github.com/redningsselskapet/nestjs-plugins/blob/master/packages/nestjs-nats-jetstream-transport/README.md Imports and registers the NatsJetStreamTransport module in the main application module, configuring the NATS server connection options for a publisher service. ```typescript // app.module.ts import { Module } from '@nestjs/common'; import { AppController } from './app.controller'; import { AppService } from './app.service'; import { NatsJetStreamTransport } from '@nestjs-plugins/nestjs-nats-jetstream-transport'; @Module({ imports: [ NatsJetStreamTransport.register({ connectionOptions: { servers: 'localhost:4222', name: 'myservice-publisher', }, }), ], controllers: [AppController], providers: [AppService], }) export class AppModule {} ``` -------------------------------- ### Handling NATS JetStream Events and Messages in NestJS Controller Source: https://github.com/redningsselskapet/nestjs-plugins/blob/master/packages/nestjs-nats-jetstream-transport/README.md Defines a NestJS controller with HTTP endpoints to trigger NATS messages and microservice handlers using @EventPattern and @MessagePattern to consume messages from NATS JetStream subjects. ```typescript // app.controller.ts import { NatsJetStreamContext } from '@nestjs-plugins/nestjs-nats-jetstream-transport'; import { Controller, Get } from '@nestjs/common'; import { Ctx, EventPattern, MessagePattern, Payload, } from '@nestjs/microservices'; import { AppService } from './app.service'; @Controller() export class AppController { constructor(private readonly appService: AppService) {} @Get() home(): string { return 'Welcome to webshop'; } @Get('/create') createOrder(): string { return this.appService.createOrder(); } @Get('/update') updateOrder(): string { return this.appService.updateOrder(); } @Get('/delete') deleteOrder(): string { return this.appService.deleteOrder(); } // request - response @Get('/sum') calc() { console.log('sum controller'); return this.appService.accumulate([1, 2, 3]); } @EventPattern('order.updated') public async orderUpdatedHandler( @Payload() data: string, @Ctx() context: NatsJetStreamContext, ) { context.message.ack(); console.log('received: ' + context.message.subject, data); } @EventPattern('order.created') public async orderCreatedHandler( @Payload() data: { id: number; name: string }, @Ctx() context: NatsJetStreamContext, ) { context.message.ack(); console.log('received: ' + context.message.subject, data); } @EventPattern('order.deleted') public async orderDeletedHandler( @Payload() data: any, @Ctx() context: NatsJetStreamContext, ) { context.message.ack(); console.log('received: ' + context.message.subject, data); } // request - response @MessagePattern({ cmd: 'sum' }) async accumulate(data: number[]): Promise { console.log('message conroller', data); return (data || []).reduce((a, b) => a + b); } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.