@mobicoop/message-broker-module
TypeScript icon, indicating that this package has built-in type declarations

2.1.2 • Public • Published

Mobicoop V3 - Message broker package

Message broker NPM package for Mobicoop V3 services. Encapsulates @golevelup/nestjs-rabbitmq, and simplifies its usage by restricting configuration to the minimum required for Mobicoop V3 (eg. only one exchange).

Requirements

  • a running RabbitMQ server

Installation

npm install --save @mobicoop/message-broker-module

Usage

Add the module in the imports section of the parent module, for example, using NestJs ConfigModule to inject values from .env:

...
import {
  MessageBrokerModule,
  MessageBrokerModuleOptions,
} from '@mobicoop/message-broker-module';
import { ConfigModule, ConfigService } from '@nestjs/config';
...
imports: [
    ConfigModule.forRoot({ isGlobal: true }),
    MessageBrokerModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: async (
        configService: ConfigService,
      ): Promise<MessageBrokerModuleOptions> => ({
        uri: configService.get<string>('MESSAGE_BROKER_URI'),
        exchange: {
            name: configService.get<string>('MESSAGE_BROKER_EXCHANGE_NAME'),
            durable: configService.get<boolean>('MESSAGE_BROKER_EXCHANGE_DURABILITY'),
        }
      }),
    }),
],
  ...

You need to set the following options :

  • uri : the uri of the message broker
  • exchange : the name of the exchange, and the durability
  • name (optional) : the name of the instance (useful for logs)

Message subscribing

To subscribe to messages, you must set handlers in the factory function :

...
import {
  MessageBrokerModule,
  MessageBrokerModuleOptions,
} from '@mobicoop/message-broker-module';
import { ConfigModule, ConfigService } from '@nestjs/config';
...
imports: [
    ConfigModule.forRoot({ isGlobal: true }),
    MessageBrokerModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: async (
        configService: ConfigService,
      ): Promise<MessageBrokerModuleOptions> => ({
        uri: configService.get<string>('MESSAGE_BROKER_URI'),
        exchange: {
            name: configService.get<string>('MESSAGE_BROKER_EXCHANGE_NAME'),
            durable: configService.get<boolean>('MESSAGE_BROKER_EXCHANGE_DURABILITY'),
        }
        handlers: {
          adCreated: {
            routingKey: 'ad.created',
            queue: 'message-broker-ad-created',
          },
        },
      }),
    }),
],
...

handlers is an object containing the handlers that will subscribe to the routingKey, on the given queue, under the form :

handlers: {
    someHandlerName: {
        routingKey: 'some.routing.key.',
        queue: 'some-queue',
    },
    anotherHandlerName: {
        routingKey: 'another.routing.key.',
        queue: 'another-queue',
    },
    ...
}

Then you need to use the @RabbitSubscribe annotation, using a routingKey defined in the handlers, eg. :

...
@RabbitSubscribe({
    name: 'someHandlerName',
})
async myHandlerFunction(message: string): {
    ...
}
...

Note that the module is available globally, you must define it only once in your project and declare all required handlers in the same place.

Message publishing

To publish messages to the broker, you must inject MessageBrokerPublisher in your service :

...
constructor(
    private readonly messageBrokerPublisher: MessageBrokerPublisher,
) {}
...

Then you can publish a message :

...
this.messageBrokerPublisher.publish(
    'my.routing.key',
    JSON.stringify({
        message: 'A nice message !',
    }),
);
...

Note that all messages are received / sent via the previously set exchange

Readme

Keywords

none

Package Sidebar

Install

npm i @mobicoop/message-broker-module

Weekly Downloads

78

Version

2.1.2

License

AGPL

Unpacked Size

61.5 kB

Total Files

39

Last publish

Collaborators

  • sbriat
  • itmobicoop