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

@aiondigital/messaging-queues

v1.1.5

Published

messaging queues based on different message brokers

Downloads

1,067

Readme

NestJS Messaging Module

This is an npm package for managing messaging in NestJS applications. It provides a way to configure messaging options and send messages from different services in your NestJS application.

Installation

You can install this package using npm or yarn:

npm install @aiondigital/messaging-queues
# or
yarn add @aiondigital/messaging-queues

Usage/Examples

To configure the module globally, you can use the MessagingModule.forRootAsync method. This allows you to set up the messaging options for your application.

import { MessagingModule } from '@aiondigital/messaging-queues';
import { ConfigurationService } from './configuration.service';

@Module({
  imports: [CommonModule],
  imports: [
    MessagingModule.forRootAsync({
      useFactory: (_config: ConfigurationService) => {
        return _config.MESSAGING_OPTIONS;
      },
      inject: [ConfigurationService],
    }),
  ],
})
export class YourModule {}

The MESSAGING_OPTIONS object should have the following fields:

  • expirationSeconds (optional): A number specifying message expiration time.
  • deadLetterExchange: The name of the dead-letter exchange.
  • deadLetterQueue: The name of the dead-letter queue.
  • brokerType: A value representing the broker type (e.g., "redis" or "rabbitmq").
  • rabbitmq (optional): Additional configuration for RabbitMQ, if brokerType is set to "rabbitmq".
  • redis (optional): Additional configuration for Redis, if brokerType is set to "redis".

Sending Messages

You can send messages from different services in your NestJS application using the MessagingService. First, you need to register a queue:

import { MessagingModule } from '@aiondigital/messaging-queues';

@Module({
  imports: [MessagingModule.registerQueue({
    name: ROUTING_KEYS.ADD_AUDIT_LOG,
    exchangeName: EXCHANGE.AUDIT,
  })],
})
export class YourModule {}

Then, in your service, you can use the MessagingService to send messages:

import { Injectable } from '@nestjs/common';
import { MessagingService } from '@aiondigital/messaging-queues';

@Injectable()
export class YourService {
  constructor(private readonly messagingService: MessagingService) {}

  sendLogs(input) {
    this.messagingService.sendMessage(ROUTING_KEYS.ADD_AUDIT_LOG, input);
  }
}

That's it! You've now set up global configuration for the messaging module and can use the MessagingService to send messages from your NestJS services.

Message Processing

Class-Level Decorator

At the class level, you can use the @Processor decorator to specify which routing key this class will be responsible for processing. Here's how you can use it:

import { Processor } from '@aiondigital/messaging-queues';

@Processor(ROUTING_KEYS.ADD_AUDIT_LOG)
export class YourMessageProcessor {
  // Your message processing methods go here
}

Function-Level Decorator

For individual message processing methods, you can use the @Process decorator to specify the details of the message processing. Here's how you can use it:

import { Process } from '@aiondigital/messaging-queues';
import { CreateAuditLogInput } from './create-audit-log.dto';

@Process({
  name: '', // Give it a unique name
  exchangeName: EXCHANGE.AUDIT,
  routingKey: ROUTING_KEYS.ADD_AUDIT_LOG,
})
async auditlog(data: CreateAuditLogInput, done: AckCallback) {
  // Your message processing logic here
  try {
    const audit = await this.auditService.create(data, ['id']);
    return audit;
  } catch (error) {
    this.#logger.error(`Failed to add audit log: [${JSON.stringify(data)}]`);
    this.#logger.error(error);
    done(error);
  }
}

You should place the @Process decorator on methods that will process messages with a specific routing key. You can have multiple methods with different routing keys within the same class.

Remember to replace the placeholders (ROUTING_KEYS.ADD_AUDIT_LOG, EXCHANGE.AUDIT, etc.)` with the actual values from your application.

Now, the YourMessageProcessor class will process messages with the routing key specified in the @Processor decorator, and the auditlog method will handle messages with the routing key specified in the @Process decorator.

License

This package is released under the MIT License