@condor-labs/sqs

This module provide and usefull helper to use the official AWS-SQS library.

Usage no npm install needed!

<script type="module">
  import condorLabsSqs from 'https://cdn.skypack.dev/@condor-labs/sqs';
</script>

README

This module provide and usefull helper to use the official AWS-SQS library.

See official documentation here.

Compatibility

The minimum supported version of Node.js is v8.

How to use it

To use the library you just need to follow the following steps Install the library with npm

npm install @condor-labs/sqs

Import the library:

const sqs = require('@condor-labs/sqs')(settings);

Credentials settings object properties

Property Default Description
accessKeyId (String) Your AWS access key ID.
secretAccessKey (String) Your AWS secret access key.
region (String) The region to send service requests to. See AWS.SQS.region for more information.
endpoint (String) The endpoint URI to send requests to. This property is optional . See AWS.SQS.endpoint for more information.

Push settings object properties

Property Default Description
queueUrl (String) The URL of the Amazon SQS queue
messageBody (String) The message to send. The maximum string size is 256 KB.
delaySeconds*(Number)* 0 The length of time, in seconds, for which to delay a specific message. Valid values: 0 to 900. Maximum: 15 minutes. Messages with a positive DelaySeconds value become available for processing after the delay period is finished. If you don't specify a value, the default value for the queue applies.
maxAttempts*(Number)* 3 max attemps to send message to the SQS
isFifo (Boolean) false Flag to indicate of the target SQS is Standard or FIFO
messageGroupId (String) . (Required if isFifo=true)
messageDeduplicationId (String) . (Required if isFifo=true)

Polling settings object properties

Property Default Description
queueUrl (String) The URL of the Amazon SQS queue
pollHandler*(Function)* Handler to process received message
emptyHandler*(Function)* null Handler to deal with empty message batches . (optional)
isCallback (Boolean) false Flag to indicate if the messages will be processed using promises or callbacks
batchSize*(Number)* 1 The number of messages to request from SQS when polling (default 1). This cannot be higher than the AWS limit of 10.
waitTimeSeconds*(Number)* 5 The duration (in seconds) for which the call will wait for a message to arrive in the queue before returning.
visibilityTimeout*(Number)* 30 The duration (in seconds) that the received messages are hidden from subsequent retrieve requests after being retrieved by a ReceiveMessage request.

Examples

constants.js

module.exports = {
    queueStandardURL: 'https://sqs.us-east-1.amazonaws.com/9999999/npm-example-sqs-standard',
    queueFIFOURL: 'https://sqs.us-east-1.amazonaws.com/999999/npm-example-sqs.fifo',
    awsCredentials: {
        accessKeyId: "AKIAJPLYT*****",
        secretAccessKey: "YfXKJz+Y5garKqKpEp******",
        region: "us-xxxx-##"
    },
    MessageBody: JSON.stringify({
        "id_owner": "222881",
        "entityId": "222881",
        "eventDate": "2017-08-19T15:43:23.784",
        "eventType": "COMPLIANCE",
        "data":
        {
            "id_user": 4087436,
            "source": {
                "code": "NEW_OWNER_OH_SPEECH"
            }
        }
    })
};

./sqs-poll-callback/index.js

const sqs = require('@condor-labs/sqs');

const {
    queueStandardURL,
    awsCredentials
} = require('./../constants');
const worker = {
    pollHandlerWithCallback: (message, cb) => {
        console.log('sqs-poll-callback OK!!!');
        cb();
        // get just one message (for testing purpose) :) 
        process.exit(1);
    },
    start: () => {

        //Standard queue
        const pollParams = {
            queueUrl: queueStandardURL,
            pollHandler: self.pollHandlerWithCallback,
            isCallback: true,
            batchSize: 1,
            waitTimeSeconds: 1,
            visibilityTimeout: 30
        };
        sqs.poll(pollParams, awsCredentials);
    }
};

// Start polling
if (!module.parent) {
    worker.start();
}

./sqs-poll-promise/index.js

const sqs = require('@condor-labs/sqs');

const {
    queueStandardURL,
    awsCredentials
} = require('./../constants');

const worker = {
    pollHandlerPromise: async message => {
        console.log('sqs-poll-promise OK!!!');
        // get just one message (for testing purpose) :) 
        process.exit(1);
    },
    start: () => {

        //Standard queue
        const pollParams = {
            queueUrl: queueStandardURL,
            pollHandler: self.pollHandlerPromise,
            isCallback: false, /* Default: false */
            batchSize: 1,
            waitTimeSeconds: 1,
            visibilityTimeout: 30
        };
        sqs.poll(pollParams, awsCredentials);
    }
};

// Start polling
if (!module.parent) {
    worker.start();
}

./sqs-push-fifo/index.js

const sqs = require('@condor-labs/sqs');

const {
    queueFIFOURL,
    awsCredentials,
    MessageBody
} = require('./../constants');

(async () => {
    let pushParams = {
        queueUrl: queueFIFOURL,
        messageBody: MessageBody,
        maxAttempts: 0, //Optional
        isFifo: true,
        messageGroupId: 'example-group-id',
        messageDeduplicationId: `messageDeduplicationId_${(new Date()).getTime()}`
    };
    // TEST PUSH in Standard queue (Promise implementation)
    let res = await sqs.push(pushParams, awsCredentials);
    console.log('sqs-push-fifo(Promise) OK!!');
    console.log('----------------------------------------------');

    // TEST PUSH in Standard queue ( CB implementation)
    pushParams.messageDeduplicationId = `messageDeduplicationId_${(new Date()).getTime()}`; // update deduplication param
    sqs.pushcb(pushParams, awsCredentials, (err, res) => {
        if (err) {
            console.log('sqs-push-fifo(callback) - err', err);
            return;
        }
        console.log('sqs-push-fifo(callback) OK!!!');
        console.log('----------------------------------------------');
        process.exit(1);
    });
})();

./sqs-push-standard/index.js

const sqs = require('@condor-labs/sqs');

const {
    queueStandardURL,
    awsCredentials,
    MessageBody
} = require('./../constants');

(async () => {
    const pushParams = {
        queueUrl: queueStandardURL,
        messageBody: MessageBody,
        delaySeconds: 0, //Optional
        maxAttempts: 0 //Optional
    };
    // TEST PUSH in Standard queue (Promise implementation)
    let res = await sqs.push(pushParams, awsCredentials);
    console.log('sqs-push-standard(Promise) OK!!!');
    console.log('----------------------------------------------');
    // TEST PUSH in Standard queue ( CB implementation)
    sqs.pushcb(pushParams, awsCredentials, (err, res) => {
        if (err) {
            console.log('sqs-push-standard(callback) - err', err);
            return;
        }
        console.log('sqs-push-standard(callback) OK!!!');
        console.log('----------------------------------------------');
        process.exit(1);
    });
})();

How to Publish

Increasing package version

You will need to update the package.json file placed in the root folder.

identify the property version and increase the right number in plus one.

Login in NPM by console.

 npm login
 [Enter username]
 [Enter password]
 [Enter email]

If all is ok the console will show you something like this : Logged in as USERNAME on https://registry.npmjs.org/.

Uploading a new version

 npm publish --access public

Ref: https://docs.npmjs.com/getting-started/publishing-npm-packages

Note: you will need to have a NPM account, if you don't have one create one here: https://www.npmjs.com/signup

Contributors

The original author and current lead maintainer of this module is the @condor-labs development team.

More about Condorlabs Here.

License

MIT