NestJS - RabbitMQ custom strategy
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
;//...{const app = await NestFactory;app;}
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.
@: stringconsole;return 'test' + data;
As for the clients, they have the same options:
;//...client =urls: `amqp://login:password@host`queue: 'test'queueOptions: durable: false;
To send a message simply use send()
function, which returns an Observable:
@: Observable<string>let msg = 'test ' + Math;console;return thisclientsend<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.