> 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/bootstrap-http-worker-mode.md).

# Bootstrap (Http, Worker mode)

#### 📘 Introduction

> A simple wrapper to speed up messaging-based app setup in NestJS.

[`@nestjstools/messaging-bootstrap`](https://www.npmjs.com/package/@nestjstools/messaging-bootstrap) It is a lightweight utility that simplifies bootstrapping a messaging-enabled NestJS application.

Built on top of [`@nestjstools/messaging`](https://www.npmjs.com/package/@nestjstools/messaging), it offers two main modes:

* An **HTTP server** with integrated messaging publisher capabilities
* A **dedicated worker/microservice** that runs only messaging consumers

***

#### ⚙️ Installation

```bash
yarn add @nestjstools/messaging-bootstrap @nestjs/microservices
```

***

#### 🚀 Quick Start

Create a file `messaging-confg.ts`

```typescript
// messaging-confg.ts

import { MessagingRabbitmqExtensionModule } from '@nestjstools/messaging-rabbitmq-extension';
import { AmqpChannelConfig, ExchangeType } from '@nestjstools/messaging';
import { MessagingModuleConfig } from '@nestjstools/messaging-bootstrap';

export const Config: MessagingModuleConfig = {
  extensions: [
    MessagingRabbitmqExtensionModule
  ],
  buses: [
    { name: 'command.bus', channels: ['async-command'] }
  ],
  channels: [
    new AmqpChannelConfig({
      name: 'async-command',
      connectionUri: 'amqp://localhost',
      exchangeName: 'command.exchange',
      bindingKeys: ['command.#'],
      exchangeType: ExchangeType.TOPIC,
      queue: 'app.command',
      enableConsumer: false,
    }),
  ],
};

```

**✅ HTTP Server with Messaging Publisher**

```ts
// main.ts
import { AppModule } from './app.module';
import { ConsoleLogger } from '@nestjs/common';
import { MessagingBootstrap } from '@nestjstools/messaging-bootstrap';
import { Config } from './messaging-confg';

async function bootstrap() {
  const app = await MessagingBootstrap.createNestApplicationWithMessaging(AppModule, {
    messaging: Config,
    nestApplicationOptions: {
      logger: new ConsoleLogger({ json: true }),
    },
  });
  await app.listen(3000);
}
bootstrap();

```

**✅ Worker/Microservice with Consumers**

```ts
// worker.ts
import { AppModule } from './app.module';
import { ConsoleLogger } from '@nestjs/common';
import { MessagingBootstrap } from '@nestjstools/messaging-bootstrap';
import { Config } from './messaging-confg';

async function bootstrap() {
  const app = await MessagingBootstrap.createNestMicroserviceWithMessagingConsumer(AppModule, {
    messaging: Config,
    nestMicroserviceOptions: {
      logger: new ConsoleLogger({ json: true }),
    },
  });
  await app.listen();
}
bootstrap();
```

***

#### 🛠️ 4. Configuration

All options are passed into the bootstrap function:

**`messaging` options:**

* `extensions`: array of messaging extension modules (e.g. RabbitMQ) (optional)
* `buses`: define one or more message buses
* `channels`: define channel configs (e.g. AMQP)

**`nestApplicationOptions` /** `nestMicroserviceOptions`**:**

* Directly passed to `NestFactory.create` or `createMicroservice`

***

#### 📄 5. Examples

**Shared Configuration Pattern**

```ts
export const messagingConfig = {
  extensions: [MessagingRabbitmqExtensionModule],
  buses: [{ name: 'command.bus', channels: ['async-command'] }],
  channels: [
    new AmqpChannelConfig({
      name: 'async-command',
      connectionUri: 'amqp://localhost',
      exchangeName: 'command.exchange',
      queue: 'app.command',
      bindingKeys: ['command.#'],
      exchangeType: ExchangeType.TOPIC,
      deadLetterQueueFeature: true,
      autoCreate: true,
      enableConsumer: false, // worker will auto-enable it
    }),
  ],
};
```

Then reuse in both `main.ts` and `worker.ts`.

***

#### ⚠️ 7. Common Pitfalls

**❌ Do not use `MessagingModule.forRoot()` manually**

> The bootstrap library already handles it for you. Including it manually will lead to **duplicate initialization** and **unexpected bugs**.

Correct usage:

```ts
// DO NOT DO THIS!
@Module({
  imports: [
    MessagingModule.forRoot({...}) // ❌ DON'T DO THIS
  ]
})
export class AppModule {}
```

Instead, let the bootstrap function handle it automatically.


---

# 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/bootstrap-http-worker-mode.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.
