npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@mobicoop/message-broker-module

v2.1.2

Published

Mobicoop V3 message broker module

Downloads

338

Readme

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