npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

simple-sqs

v1.0.0

Published

A simple wrapper around the AWS SQS queue service

Downloads

17

Readme

simple-sqs

A very opinionated wrapper around the SQS part of the official aws-sdk module. This module basically takes care of the following mondane tasks related to queue management:

  • It will poll the queue for you
  • It will notify you of new messages on the queue using the EventEmitter pattern
  • It will delete messages from the queue after you've finished processing them
  • It will wait for 5 seconds if the queue for some reason returns an error before re-polling
  • It expects that the message body is json and will parse it for you
  • If the message body cannot be parsed, it's considered an unrecoverable error and the message is deleted from the queue while emitting an error event. You can override this behaviour and ignore parser errors with the option ignoreParseErrors

Build status js-standard-style

Installation

npm install simple-sqs --save

Usage

Simple example using a convinient callback function to get messages:

var sqs = require('simple-sqs')()

var queueUrl = 'https://sqs.us-east-1.amazonaws.com/12345/my-queue'

sqs(queueUrl, function (msg, done) {
  console.log('Received a new message with id:', msg.MessageId)
  console.log(msg.Body)

  // acknowledge the message by calling the done callback
  // (this will delete it from the queue)
  done()
})

Or, if you do not provide the optional 2nd callback argument, you can always listen for messages on the returned EventEmitter:

var queue = sqs(queueUrl)

queue.on('message', function (msg, done) {
  console.log('Received a new message with id:', msg.MessageId)
  done()
})

API

The simple-sqs module exposes an initializer function that takes a single options argument to configure SQS. Besides a few custom options that are described below, you can supply the regular aws-sdk configuration options. See the official aws-sdk SQS documentation for details. If no argument is provided simple-sqs defaults to { apiVersion: '2012-11-05', region: 'us-east-1' }:

var opts = {...} // SQS config options
var sqs = require('simple-sqs')(opts)

Options:

  • wait - Boolean, defaults to false. If set to true, the module will wait for all messages to finish processing before polling again. Takes precedence over pollInterval if messages take longer to process
  • pollInterval - Integer, defaults to 0 which means that the queue will be polled immediately after a batch of messages have been received unless wait is set to true
  • ignoreParseErrors - Boolean, defaults to false. If a message cannot be parsed, it's normally considered a permanent failure and the message will be deleted from the queue. To overwrite this behaviour, set this option to true.

The returned sqs value is a queue setup function that takes two arguments and returns a queue object:

var queue = sqs(url[, callback])

Arguments:

  • url - The SQS queue URL
  • callback - Optional callback which will be attached as a listener on message events

Returns:

The returned queue object is an EventEmitter that can emit the following two events:

  • message
  • error

Event: 'message'

Emitted every time a new message is received on the SQS queue.

queue.on('message', function (msg, done) {
  // ...
})

Arguments:

  • msg - The message received on the queue
  • done - A callback function

If the callback function isn't called, the message will be returned to the queue after a configured timeout. You would therefore normally want to call the callback if you have sucessfully processed the message.

Optinally you can call the callback with an error as the 1st arguemnt. In this special case the message will not be deleted and will be made available again as if the callback was never called. This feature is mostly a convenience feature so that you can simplify the deletion processes if the success depends on an async call, e.g:

queue.on('message', function (msg, done) {
  // Process the message - in this case we try to insert it into a Mongo
  // database:

  db.foo.insert(msg.Body, done)

  // If the `insert` function calls the callback without an error, the
  // message will be deleted from the queue

  // If called _with_ an error, the message will NOT be deleted and the
  // error will be emitted back so we can log it
})

queue.on('error', function (err) {
  // The error returned by the `insert` function will end up here
  console.log('Ups, something went wrong!')
  cosnole.log(err.stack)
})

Event: 'error'

Emitted if the module encounters an error.

queue.on('error', function (err) {
  // ...
})

Debug output

You can enable debug output using the DEBUG=simple-sqs environment variable.

Testing

The tests require a real SQS queue to run. Set it up and use the ~/.aws/credentials or EC2 configured role to give the module the proper access.

Then run:

npm test

License

MIT