1.2.0 • Published 2 years ago

@happyaws/sqs-consumer v1.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

Build SQS-based applications without the boilerplate. Based on AWS-SDK v3.
This package build on top of xstate and its Actor Model,

Installation

yarn add @happyaws/sqs-consumer @aws-sdk/client-sqs
npm install --save @happyaws/sqs-consumer @aws-sdk/client-sqs

Example

import { Message, SQSClient } from '@aws-sdk/client-sqs';
import { Consumer } from '@happyaws/sqs-consumer';

const main = async () => {
  const sqsClient = new SQSClient({
    endpoint: 'http://localhost:4566',
  });
  const queueUrl = 'http://localhost:4566/000000000000/test';

  const consumer = Consumer.create({
    queueUrl,
    sqsClient,
    maxNumberOfMessages: 3,
    waitTimeSeconds: 5,
    deleteMessage: true,
    receiveMessageFailedDelay: 2000,
    handleError: async (error, message?) => {
      console.log('error', error, message);
    },
    handleMessages: async (messages) => {
      const payloads = messages.map((e) => ({
        ...JSON.parse(e.Body),
        messageId: e.MessageId,
        receiptHandle: e.ReceiptHandle,
      }));

      return payloads;
    },
    handleOnEmptyMessageRecived: (res) => {
      console.log('empty', res);
    },
    handleOnMessageDeleted: (res) => {
      console.log('delete', res.Successful);
    },
    handleOnProcessingError: (error) => {
      console.log('handleOnProcessingError', error);
    },
  });

  consumer.start();
};

main();

Features

  • The queue is polled continuously for messages using long polling.
  • Message will auto delete from the queue once the handler function has completed successfully, only if deleteMessage is true.
  • Exposed the lifecycle event to let you visual the status.

TODO

x Support batch message ( > 1 && <= 10 )
x Add test cases
x Able to delete the messages if process success

1.2.0

2 years ago

1.1.0

2 years ago

1.0.0

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago