Git Product home page Git Product logo

nestjs-rmq's Introduction

NestJS - RabbitMQ custom strategy

alt cover

npm version npm version npm version npm version Build Status

This module is a custom strategy for NestJS microservice library. It allows you to use RabbitMQ as a transport for microservice messages. Learn about NestJS here.

To generate your microservice, just use @nestjs/cli:

nest new my-microservice

Then install nestjs-rmq:

npm i nestjs-rmq

Each one of your services can be a server and/or a client. To start server, change bootstrap() function in main.ts

import { ServerRMQ } from 'nestjs-rmq';

//...

async function bootstrap() {
  const app = await NestFactory.createMicroservice(AppModule, {
    strategy: new ServerRMQ({
      urls: [`amqp://login:password@host`],
      queue: 'test',
      queueOptions: { durable: false }
    })
  });
  app.listen(() => console.log('Server is listening'));
}

Options are: urls (string[]) - Connection urls to one or more RabbitMQ instance. Each url contains user, password and host. queue (string) - Name of queue, your server will lusten to. prefetchCount | number | Number of prefetched messages. You can read more here. isGlobalPrefetchCount (boolean) - You can read more here. queueOptions (object) - Additional queue options. You can read more here.

After initializing server you can use @MessagePattern in controllers as described in NestJS docs.

@MessagePattern({ cmd: 'test' })
test(data: string): string {
    console.log('Server got: ' + data);
    return 'test' + data;
}

As for the clients, they have the same options:

import { ClientRMQ } from 'nestjs-rmq';

//...

client = new ClientRMQ({
    urls: [`amqp://login:password@host`],
    queue: 'test',
    queueOptions: { durable: false }
});

To send a message simply use send() function, which returns an Observable:

@Get('a')
a(): Observable<string> {
    let msg = 'test ' + Math.random();
    console.log('Client sent: ' + msg);
    return this.client.send<string, string>({ cmd: 'test' }, msg);
}

Breaking changes from v0.1.0 to v0.1.1

You need to use urls option instead of url and pass string[] instead of string for cluster support.

nestjs-rmq's People

Contributors

alaricode avatar

Watchers

 avatar  avatar  avatar

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.