0.0.4 • Published 5 years ago
loopback4-sqs-consumer v0.0.4
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-consumerUsage
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.