@aiondigital/messaging-queues

中文文档
1.1.5发布于 2024/04/30ISCTypeScriptCommonJS近 30 天下载 2194

messaging queues based on different message brokers

原始使用文档
文档目录7 节
  1. Installation
  2. Usage/Examples
  3. Sending Messages
  4. Message Processing
  5. Class-Level Decorator
  6. Function-Level Decorator
  7. License

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

版本与发布记录

@aiondigital/messaging-queues 共发布 14 个版本,首次发布于 2023/10/15;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.1.5latest2024/04/302 年前
1.1.42024/04/212 年前
1.1.32024/02/122 年前
1.1.02023/10/173 年前
1.0.92023/10/173 年前
1.0.82023/10/173 年前

关于 @aiondigital/messaging-queues 的常见问题

@aiondigital/messaging-queues 是什么?

messaging queues based on different message brokers

如何安装 @aiondigital/messaging-queues?

在项目目录运行 npm install @aiondigital/messaging-queues,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@nestjs/common(^10.2.7)、@nestjs/core(^10.2.7)、amqplib(^0.10.3)、bull(^3.3 || ^4.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@aiondigital/messaging-queues 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@aiondigital/messaging-queues 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@aiondigital/messaging-queues 可以商用吗?

@aiondigital/messaging-queues 使用 ISC 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@aiondigital/messaging-queues 的下载量有多少?

根据 npm 官方下载统计,@aiondigital/messaging-queues 在 2026/10/05 – 2026/11/03 这 30 天内被下载 2,194 次,日均约 73 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@aiondigital/messaging-queues 还在维护吗?

最新版本 1.1.5 发布于 2024/04/30(2 年前);至今共发布 14 个版本,其中过去 12 个月发布了 0 个;首次发布于 2023/10/15。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。