Consumer as the background process
⚠️ Before You Continue
It's recommended to read the Bootstrap Guide first. It provides a higher-level, smarter approach for managing messaging in both HTTP server and worker (microservice) modes.
This page is useful if you're configuring consumers manually or outside the bootstrap utility.
🧾 Consumer as a Background Process
In your messaging configuration, you have the ability to run certain channels in background consumer mode, meaning the application will continuously poll and process messages from a queue (e.g., RabbitMQ).
This behavior is explicitly controlled using the enableConsumer flag inside the channel configuration.
✅ Enabling or Disabling Consumers
Each channel can individually opt into message consumption:
new AmqpChannelConfig({
name: 'amqp-command.channel',
connectionUri: 'amqp://guest:guest@localhost:5672/',
exchangeName: 'book_shop.exchange',
bindingKeys: ['book_shop.#'],
exchangeType: ExchangeType.TOPIC,
queue: 'book_shop.command',
enableConsumer: process.env.CONSUMER_ENABLED === 'true' ?? false, // ← starts the background consumer
})Setting enableConsumer: true allows the messaging module to automatically listen to that queue and handle incoming messages to the proper handlers in your NestJS app.
⚙️ Running the Consumer as a Dedicated Microservice
In addition to toggling enableConsumer in your channel config, you can fully isolate message consumption into its own process using NestJS microservice mode.
This is useful when you want to separate HTTP requests from background processing, scale them independently, or run consumers in isolated environments (e.g., workers).
🔁 From HTTP App to Microservice Consumer
Your standard main.ts for a typical HTTP NestJS app might look like this:
You can create worker.ts a microservice-based consumer by doing the following:
✅ Microservice Consumer Entry
Last updated