Nestjstools Messaging Docs
  • Introduction
    • What is this library?
    • Supported message brokers
  • Getting Started
    • Installation
    • Initialize Module
    • Message Handler
    • Disaptch a message
  • Components
    • Message Handlers
    • Normalizers
    • Exception Listeners
    • Middlewares
    • Message Bus
    • Channel
  • Broker integration
    • RabbitMQ
    • Redis
    • Google PubSub
    • Amazon SQS
    • Nats
  • Best practice
    • CQRS based on RabbitMQ
    • Create wrapper class for Message Bus
    • Consumer as the background process
Powered by GitBook
On this page
  • ๐Ÿงพ Consumer as a Background Process
  • โš™๏ธ Running the Consumer as a Dedicated Microservice
Export as PDF
  1. Best practice

Consumer as the background process

๐Ÿงพ 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.

If you set:

enableConsumer: false

This makes it super easy to spin up separate workers or scale differently for message producers and consumers.

Great addition! Here's how you can present this as part of your Consumer as a Background Process page, demonstrating how to run the consumer logic separately using NestJS microservices:


โš™๏ธ 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:

// Standard HTTP entry point
process.env.CONSUMER_ENABLED = 'false';
import { NestFactory } from '@nestjs/core';
import { BookModule } from './book.module';

async function bootstrap() {
  const app = await NestFactory.create(BookModule.forRoot());
  await app.listen(process.env.port ?? 3000);
}
bootstrap();

You can create worker.ts a microservice-based consumer by doing the following:

โœ… Microservice Consumer Entry

// microservice-main.ts
process.env.CONSUMER_ENABLED = 'true'; // ensures consumers are enabled via config

import { NestFactory } from '@nestjs/core';
import { Transport, MicroserviceOptions } from '@nestjs/microservices';
import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.createMicroservice<MicroserviceOptions>(
    AppModule,
    {
      transport: Transport.TCP,
    },
  );
  await app.listen();
}
bootstrap();

๐Ÿง  Tip: Using Transport.TCP here simply satisfies the NestJS microservice API; actual messaging will still be handled by RabbitMQ, SQS, Redis, etc. You can safely ignore it or replace it with Transport.NONE if you donโ€™t need internal messaging.

PreviousCreate wrapper class for Message Bus

Last updated 9 days ago