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);
}
You need to use urls
option instead of url
and pass string[]
instead of string
for cluster support.