Bootstrap (Http, Worker mode)

📘 Introduction

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

@nestjstools/messaging-bootstrap It is a lightweight utility that simplifies bootstrapping a messaging-enabled NestJS application.

Built on top of @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

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

🚀 Quick Start

Create a file messaging-confg.ts

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

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

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

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:

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

Instead, let the bootstrap function handle it automatically.

Last updated