Giter VIP home page Giter VIP logo

nats-strategy's Introduction

Description

NATS v2 strategy and client module for Nest

Test CI npm monthly downloads

Installation

$ npm i --save @nestjs-ex/nats-strategy

Usage

To use the Nats transporter, pass the following options object to the createMicroservice() method:

import { NestFactory } from '@nestjs/core';
import { NatsStrategy } from '@nestjs-ex/nats-strategy';
import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.createMicroservice(
    AppModule,
    {
      strategy: new NatsStrategy({
        servers: '127.0.0.1:4222',
        user: 'jenny',
        pass: '867-5309',
      })
    },
  );
  app.listen(() => console.log('Microservice is listening'));
}
bootstrap();

Client

To create a client instance with the NatsClientModule, import it and use the register() method to pass an options object with the same properties shown above in the createMicroservice() method.

@Module({
  imports: [
    NatsClientModule.register({
      servers: '127.0.0.1:4222',
      user: 'jenny',
      pass: '867-5309',
      name: 'example-client'
    }),
  ]
  ...
})

Once the module has been imported, we can inject an instance of the NatsClient shown above

constructor(
  private client: NatsClient
) {}

Quite often you might want to asynchronously pass your module options instead of passing them beforehand. In such case, use registerAsync() method, that provides a couple of various ways to deal with async data.

1. Use factory

NatsClientModule.registerAsync({
  useFactory: () => ({
    servers: '127.0.0.1:4222',
    user: 'jenny',
    pass: '867-5309',
    name: 'example-client'
  })
});

Obviously, our factory behaves like every other one (might be async and is able to inject dependencies through inject).

NatsClientModule.registerAsync({
  imports: [ConfigModule],
  useFactory: async (configService: ConfigService) => ({
    servers: configService.getString('NATS_SERVERS'),
    name: configService.getString('NATS_NAME')
  }),
  inject: [ConfigService],
}),

2. Use class

NatsClientModule.registerAsync({
  useClass: NatsClientConfigService
});

Above construction will instantiate NatsClientConfigService inside NatsClientModule and will leverage it to create options object.

class NatsClientConfigService implements NatsClientOptionsFactory {
  createNatsClientOptions(): NatsClientModuleOptions {
    return {
      servers: '127.0.0.1:4222',
      user: 'jenny',
      pass: '867-5309',
      name: 'example-client'
    };
  }
}

3. Use existing

NatsClientModule.registerAsync({
  imports: [ConfigModule],
  useExisting: ConfigService,
}),

It works the same as useClass with one critical difference - NatsClientModule will lookup imported modules to reuse already created ConfigService, instead of instantiating it on its own.

Stay in touch

License

Nest is MIT licensed.

nats-strategy's People

Contributors

panoti avatar renovate-bot avatar renovate[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

vudc

nats-strategy's Issues

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

Location: renovate.json
Error type: The renovate configuration file contains some invalid settings
Message: timezone: Invalid schedule: Unsupported timezone ICT

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Awaiting Schedule

These updates are awaiting their schedule. Click on a checkbox to get an update now.

  • chore(deps): update dependency @types/node to v20.12.12
  • chore(deps): update typescript-eslint monorepo to v7.9.0 (@typescript-eslint/eslint-plugin, @typescript-eslint/parser)

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/development.yml
  • actions/checkout v4
  • actions/setup-node v4
  • actions/checkout v4
  • actions/setup-node v4
  • nats 2-alpine
.github/workflows/release.yml
  • actions/checkout v4
  • actions/setup-node v4
npm
package.json
  • nanoid 3.3.7
  • nats ^2.18.0
  • @commitlint/cli 19.3.0
  • @commitlint/config-angular 19.3.0
  • @nestjs/common 10.3.8
  • @nestjs/core 10.3.8
  • @nestjs/microservices ^10.2.8
  • @nestjs/platform-express ^10.2.8
  • @nestjs/testing 10.3.8
  • @types/jest 29.5.12
  • @types/node 20.12.11
  • @typescript-eslint/eslint-plugin 7.8.0
  • @typescript-eslint/parser 7.8.0
  • chai ^4.3.10
  • eslint 8.57.0
  • eslint-config-prettier 9.1.0
  • eslint-plugin-import 2.29.1
  • husky 9.0.11
  • jest 29.7.0
  • lint-staged 15.2.2
  • prettier 3.2.5
  • reflect-metadata 0.2.2
  • release-it 17.2.1
  • rimraf 5.0.7
  • supertest ^7.0.0
  • ts-jest 29.1.2
  • typescript 5.4.5
  • @nestjs/common ^7.6.15 || ^8.0.0 || ^9.0.0 || ^10.0.0
  • @nestjs/microservices ^7.6.15 || ^8.0.0 || ^9.0.0 || ^10.0.0

  • Check this box to trigger a request for Renovate to run again on this repository

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.