2.1.2 • Published 4 months ago

@mobicoop/message-broker-module v2.1.2

Weekly downloads
-
License
AGPL
Repository
gitlab
Last release
4 months ago

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

2.1.2

4 months ago

2.1.1

7 months ago

2.1.0

7 months ago

2.0.0

9 months ago

1.2.0

11 months ago

1.1.0

11 months ago

1.0.6

11 months ago

1.0.5

11 months ago

1.0.4

11 months ago

1.0.3

11 months ago

1.0.1

11 months ago

1.0.0

11 months ago