Skip to content
This repository has been archived by the owner on Oct 1, 2023. It is now read-only.
nestjs-sqs / 1.1.0-lmnd.7

nestjs-sqs 1.1.0-lmnd.7

Install from the command line:
Learn more about npm packages
$ npm install @lemonade-hq/nestjs-sqs@1.1.0-lmnd.7
Install via package.json:
"@lemonade-hq/nestjs-sqs": "1.1.0-lmnd.7"

About this version

nestjs-sqs

Test npm version

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 uses 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 @ssut/nestjs-sqs

Quick Start

Register module

Just register this module:

@Module({
  imports: [
    SqsModule.register({
      consumers: [],
      producers: [],
    }),
  ],
})
class AppModule {}

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

  • Use factory
SqsModule.registerAsync({
  useFactory: () => {
    return {
      consumers: [],
      producers: [],         
    };
  },
});
  • Use class
SqsModule.registerAsync({
  useClass: SqsConfigService,
});
  • Use existing
SqsModule.registerAsync({
  imports: [ConfigModule],
  useExisting: ConfigService,
});

Decorate methods

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

@Injectable()
export class AppMessageHandler {
  @SqsMessageHandler(/** name: */ 'queueName', /** batch: */ false)
  public async handleMessage(message: AWS.SQS.Message) {
  }
  
  @SqsConsumerEventHandler(/** name: */ 'queueName', /** eventName: */ 'processing_error')
  public onProcessingError(error: Error, message: AWS.SQS.Message) {
    // report errors here
  }
}

Produce messages

export class AppService {
  public constructor(
    private readonly sqsService: SqsService,
  ) { }
  
  public async dispatchSomething() {
    await this.sqsService.send(/** name: */ 'queueName', {
      id: 'id',
      body: { ... },
      groupId: 'groupId',
      deduplicationId: 'deduplicationId',
      messageAttributes: { ... },
      delaySeconds: 0,
    });
  }
}

Configuration

See here, and note that we have same configuration as bbc/sqs-consumer's. In most time you just need to specify both name and queueUrl at the minimum requirements.

License

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

Details


Assets

  • nestjs-sqs-1.1.0-lmnd.7-npm.tgz

Download activity

  • Total downloads 11,110
  • Last 30 days 758
  • Last week 194
  • Today 28