Create wrapper class for Message Bus

🚀 Messaging Wrapper Module

This wrapper provides a reusable, centralized setup for working with the @nestjstools/messaging library. It encapsulates sync-command bus into injectable services that can be easily used across your application.


📦 Purpose

Instead of configuring your message buses and channels in every module, this wrapper:

  • Exposes SyncCommandBus as injectable services

  • Ensures clean separation of sync/async responsibilities

  • Keeps your controller/service code lean and focused


import { IMessageBus, RoutingMessage } from '@nestjstools/messaging';

/**
 * A simplified interface for dispatching messages through a configured message bus.
 * Encapsulates routing logic to keep controllers and services clean.
 */
export class InternalMessageBus {
  constructor(private readonly messageBus: IMessageBus) {}

  /**
   * Dispatch a message using the given routing key.
   *
   * @param message The payload (command/event/etc.)
   * @param routingKey The message routing key used by the bus
   */
  async dispatch(message: object, routingKey: string): Promise<void> {
    await this.messageBus.dispatch(new RoutingMessage(message, routingKey));
  }
}

🧱 Module Setup


🧩 Dependency Injection Tokens

These are helper tokens to inject the appropriate bus:


🧪 Usage in a Controller

Example of dispatching a command using the injected @SyncCommandBus :

Last updated