@gemunion/nestjs-sqs
TypeScript icon, indicating that this package has built-in type declarations

4.0.2 • Public • Published

NestJS SQS

Tested with: AWS SQS and ElasticMQ.

NestJS SQS is a project to make SQS easier to use and control some required flows with NestJS. This module provides decorator-based message handling suited for simple use.

This library internally relies on bbc/sqs-producer and bbc/sqs-consumer, and implements some more useful features on top of the basic functionality given by them.

Installation

$ npm i --save @gemunion/nestjs-sqs

Quick Start

Register custom transport

Just like you register any other microservice

import { SQSClient } from "@aws-sdk/client-sqs";
import { SqsServer } from "@gemunion/nestjs-sqs";

const sqs = new SQSClient({
  endpoint: "http://localhost:9324",
  region: "none",
  credentials: {
    accessKeyId: "x",
    secretAccessKey: "x",
  },
});

app.connectMicroservice({
  strategy: new SqsServer({
    consumerOptions: {
      sqs,
      queueUrl: "http://localhost:9324/queue/producer.fifo",
    },
    producerOptions: {
      sqs,
      queueUrl: "http://localhost:9324/queue/consumer.fifo",
    },
  }),
});

Decorate methods

You need to decorate methods in your NestJS controller in order to have them be automatically attached as event handlers for incoming SQS messages:

@Controller()
export class SqsController {
  @MessagePattern(MESSAGE_TYPE)
  public handleMessage(message: any): Promise<any> {
    // do something, return result
  }

  @EventPattern(EVENT_TYPE)
  public handleEvent(event: any): Promise<void> {
    // do something
  }
}

Produce messages

import { SQSClient } from "@aws-sdk/client-sqs";
import { SqsClient, SQS_SERVICE } from "@gemunion/nestjs-sqs";

const sqs = new SQSClient({
  endpoint: "http://localhost:9324",
  region: "none",
  credentials: {
    accessKeyId: "x",
    secretAccessKey: "x",
  },
});

@Module({
  imports: [
    ClientsModule.register([
      {
        name: SQS_SERVICE,
        customClass: SqsClient,
        options: {
          consumerUrl: "http://localhost:9324/queue/consumer.fifo",
          producerUrl: "http://localhost:9324/queue/producer.fifo",
          sqs,
        },
      },
    ]),
  ],
  providers: [AppService],
})
class AppModule {
}

export class AppService {
  constructor(
    @Inject(SQS_SERVICE)
    private readonly sqsClientProxy: ClientProxy,
  ) {
  }

  public dispatch(): Promise<void> {
    void this.client.emit(EVENT_NAME, {});
  }
}

Code quality

Terminal 1

java -Dconfig.file=.github/build/elasticmq.conf -jar elasticmq-server-1.3.9.jar

Terminal 2

npm t

License

This project is licensed under the terms of the MIT license.

Readme

Keywords

none

Package Sidebar

Install

npm i @gemunion/nestjs-sqs

Weekly Downloads

8,342

Version

4.0.2

License

MIT

Unpacked Size

39.4 kB

Total Files

36

Last publish

Collaborators

  • ubudragon
  • trejgun