> For the complete documentation index, see [llms.txt](https://docs.nestjstools.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.nestjstools.com/best-practice/consumer-as-the-background-process.md).

# Consumer as the background process

> ⚠️ **Before You Continue**
>
> It's recommended to read the [Bootstrap ](/best-practice/bootstrap-http-worker-mode.md)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:

```ts
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:

```ts
// 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

```ts
// 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();
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.nestjstools.com/best-practice/consumer-as-the-background-process.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
