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

@saiuttej/nestjs-sqs-queue

v0.0.2

Published

A NestJS module to work with AWS SQS queues

Downloads

156

Readme

Installation

Install via NPM:

npm install @saiuttej/nestjs-sqs-queue

Install via Yarn:

yarn add @saiuttej/nestjs-sqs-queue

Install via PNPM:

pnpm add @saiuttej/nestjs-sqs-queue

Quick Start

Register Module

@Module({
  imports: [
    SQSQueueModule.forRoot({
      queues: [],
    }),
  ],
})
class AppModule {}

Quite often you might want to asynchronously pass module options instead of passing them beforehand. In such case, use forRootAsync() method like many other Nest.js libraries.

@Module({
  imports: [
    SQSQueueModule.forRootAsync({
      useFactory: (...deps) => {
        return {
          clients: [],
        };
      },
      inject: [...dependencies],
    }),
  ],
})
class AppModule {}

Module Configuration

import { Module } from '@nestjs/common';
import { QueueExecutionType, SQSQueueModule } from '@saiuttej/nestjs-sqs-queue';
import { AppController } from './app.controller';
import { AppService } from './app.service';

@Module({
  imports: [
    SQSQueueModule.forRoot({
      queues: [
        {
          key: 'unique-key-for-queue',
          queue: {
            accountId: 'aws-account-id',
            queueName: 'queue-name',
            region: 'aws-region',
          },
        },
      ],
    }),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}
  • key* - Unique key for the queue, used to identify the queue, should be unique across all queues in the application.
  • queue* - Queue configuration object
    • accountId* - AWS Account ID
    • queueName* - SQS Queue Name
    • region* - AWS Region
    • useQueueUrlAsEndpoint - Use Queue URL as endpoint for producer and consumer
  • sqsConfig - AWS SQSClient constructor options,
  • autoCreateQueue - Auto create queue if not exists (default: false)
  • consumerOptions - AWS SQSConsumer options
  • producerOptions - AWS SQSProducer options
  • executionTypes - Execution types (default: [QueueExecutionType.RECEIVE, QueueExecutionType.SEND])

Producer

import { Injectable } from '@nestjs/common';
import { SQSQueueService } from '@saiuttej/nestjs-sqs-queue';

@Injectable()
export class AppService {
  constructor(private readonly sqsQueueService: SQSQueueService) {}

  async sendMessage() {
    await this.sqsQueueService.send('unique-key-for-queue', {
      id: '123',
      body: 'message-body',
    });
  }
}

SQSQueueService provides the send method to send messages to the queue. send method takes two arguments:

  • key* - Unique key for the queue
  • message* - It can be a String, Message Object, or Array of Message Objects or Strings

Consumer

Processing a single message

import { Injectable } from '@nestjs/common';
import { OnQueueMessage } from '@saiuttej/nestjs-sqs-queue';

@Injectable()
export class AppService {
  @OnQueueMessage({ key: 'my-sqs-queue' })
  async queueHandler(message: Message) {
    console.log(message);
  }
}

Processing multiple messages

import { Injectable } from '@nestjs/common';
import { OnQueueMessage } from '@saiuttej/nestjs-sqs-queue';

@Injectable()
export class AppService {
  @OnQueueMessage({ key: 'my-sqs-queue', batch: true })
  async queueHandler(message: unknown | unknown[] | Message | Message[]) {
    console.log(message);
  }
}

OnQueueMessage decorator is used to listen to messages from the queue. OnQueueMessage decorator takes an object with the following properties:

  • key* - Unique key for the queue
  • batch - Boolean value to indicate whether to process messages in batch or not (default: false)

By default the handler function, which is annotated with the OnQueueMessage decorator, it will receive the SQS message object(s) to the function. But you can also customize the SQS message object(s) by using messageFormatter in consumerOptions, when configuring the queue.

But you need to remember that if the response return by the handler function is not void or Promise<void>, then the response should be an Object having the parameter MessageId, if it is batch operation then the response should be an Array of Objects, each Object having the parameter MessageId. This is required to delete the message from the queue after processing.

Note: Only one handler can be registered for a queue key.

Contributing

Contributions welcome! See Contributing.

Author

B Sai Uttej

License

Licensed under the MIT License - see the LICENSE file for details.