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
β Worker/Microservice with Consumers
π οΈ 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 buseschannels: define channel configs (e.g. AMQP)
nestApplicationOptions / nestMicroserviceOptions:
Directly passed to
NestFactory.createorcreateMicroservice
π 5. Examples
Shared Configuration Pattern
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:
Instead, let the bootstrap function handle it automatically.
Last updated