loopback4-sqs-consumer
TypeScript icon, indicating that this package has built-in type declarations

0.0.4 • Public • Published

loopback4-sqs-consumer

This is a loopback-next extension for consuming events from AWS SQS Queue. This extension is using sqs-consumer for polling messages from SQS Queue, so please check for more about configurations and usages.

Install

npm install --save loopback4-sqs-consumer

Usage

In order to use this component into your LoopBack application, please follow below steps.

Add component to application.

// application.ts
import {SQS} from 'aws-sdk';
import {SqsConsumerBindings, SqsConsumerComponent, SqsConsumerConfig} from 'loopback4-sqs-consumer';
....
 
export class SQSConsumerApplication extends BootMixin(
  ServiceMixin(RepositoryMixin(RestApplication)),
) {
  constructor(options: ApplicationConfig = {}) {
    ....
 
    const sqsConfig: SqsConsumerConfig = {
      sqs: new SQS({region: 'ap-south-1'})
    }
 
    this.configure(SqsConsumerBindings.COMPONENT).to(sqsConfig)
    this.component(SqsConsumerComponent)
    ....
  }
}

NOTE: Component need to configure with AWS SQS object.

Create provider for consuming SQS Events

We need to create providers to consume events. Providers are decorated with consumeQueue, which will bind to application context, so it can be managed by the SqsConsumerComponent life cycles including start and stop polling.

import {Provider} from '@loopback/core';
import {consumeQueue, SqsSubscriber} from 'loopback4-sqs-consumer';
 
@consumeQueue({
  queueUrl: 'https://sqs.<region>.amazonaws.com/<account-id>/<queue-name>'
})
export class TestQueueSubscriberProvider implements Provider<SqsSubscriber> {
  constructor() {}
 
  async value() {
    return this.action.bind(this);
  }
 
  async action(message: any) {
    console.log('action', message)
  }
}
 

Please click here medium post.

License

MIT

Package Sidebar

Install

npm i loopback4-sqs-consumer

Weekly Downloads

23

Version

0.0.4

License

none

Unpacked Size

32.5 kB

Total Files

41

Last publish

Collaborators

  • rahulrkr