Git Product home page Git Product logo

fastify-sqs-consumer's Introduction

fastify sqs-consumer

NPM version CI workflow TypeScript

Description

This plugin for fastify 4.x allows you to consume messages from AWS SQS queues. On fastify shutdown a simple wait function is called to wait for the end of the processing of the messages in progress.

Warning
To use this plugin, you must have correctly configured your AWS credentials.

Install

npm i @fgiova/fastify-sqs-consumer

Usage

const fastify = require("fastify")()

fastify.register(require("fastify-sqs-consumer"), [
    {
        url: "https://sqs.us-east-1.amazonaws.com/123456789012/MyQueue",
        waitTimeSeconds: 20,
        timeout: 10_000,
        batchSize: 10,
        handlerFunction: async (message, fastify) => {
            return true;
        }
    }
]);

Options

Options are an array of objects with the following properties (one more for each queue):

Option Type Description
url* string The URL of the Amazon SQS queue from which messages are received.
waitTimeSeconds number The duration (in seconds, default 20s) for which the call waits for a message to arrive in the queue before returning. If a message is available, the call returns sooner than WaitTimeSeconds. If no messages are available and the wait time expires, the call returns successfully with an empty list of messages.
timeout number The duration before the message is considered as failed: default 90000ms.
batchSize number The maximum number of messages to return. Amazon SQS never returns more messages than this value (however, fewer messages might be returned). Valid values: 1 to 10. Default: 1.
handlerFunction* function The function that will be called for each message.
messageAttributeNames string[] Array of caught message's attributes for each message
attributeNames string[] Array of caught queue's attributes for each message
events object Events functions for the consumer (detail in next table)
sqs SQSClient Initialized SQS Client (useful for testing sessions)

handlerFunction

Handler function is an async function called for each message received from the queue. Each Error thrown by the function is caught and the message is not deleted from the queue. Otherwise, the message is deleted from the queue.

Events

Event name Arguments Description
error error Object Fired when a general error occurs
processingError error Object, message SQS.Message Fired when an error occurs pending message handling
timeoutError error Object, message SQS.Message Fired when processing of message exceed timeout duration
If function return false message are discarded, otherwise message is bounced to queue.
messageReceived message SQS.Message Fired when message is received
messageProcessed message SQS.Message Fired when message is successfully processed
responseProcessed void Fired after one batch of items (up to batchSize) has been successfully processed.
stopped void Fired when the consumer finally stops its work.
empty void Fired when the queue is empty (All messages have been consumed).

License

Licensed under MIT.

Acknowledgements

This project is kindly sponsored by: isendu Srl www.isendu.com

fastify-sqs-consumer's People

Contributors

fgiova avatar semantic-release-bot avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.