@saiuttej/nestjs-sqs-queue
v0.0.2
Published
A NestJS module to work with AWS SQS queues
Downloads
156
Maintainers
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 objectaccountId
* - AWS Account IDqueueName
* - SQS Queue Nameregion
* - AWS RegionuseQueueUrlAsEndpoint
- 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 optionsproducerOptions
- AWS SQSProducer optionsexecutionTypes
- 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 queuemessage
* - 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 queuebatch
- 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.