RabbitMQ

The RabbitMQ is the most widely deployed open source message broker.

Installation

Before we start, we have to install required packages:

  1. $ npm i --save amqplib amqp-connection-manager

Transporter

In order to switch to RabbitMQ transporter, we need to modify an options object passed to the createMicroservice() method.

  1. @@filename(main)
  2. const app = await NestFactory.createMicroservice(ApplicationModule, {
  3. transport: Transport.RMQ,
  4. options: {
  5. urls: [`amqp://localhost:5672`],
  6. queue: 'cats_queue',
  7. queueOptions: { durable: false },
  8. },
  9. });

info Hint Transport enumerator is imported from the @nestjs/microservices package.

Options

There are a bunch of available options that determine a transporter behavior.

urls Connection urls
queue Queue name which your server will listen to
prefetchCount Sets the prefetch count for the channel
isGlobalPrefetchCount Enables per channel prefetching
queueOptions Additional queue options. They are well-described here
socketOptions Additional socket options. They are well-described here