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-queue

v0.0.5

Published

Lightweight API wrapper around Amazon's (AWS) SQS

Downloads

7

Readme

#Simple SQS Queue

NPM

simple-sqs-queue is a lightweight wrapper around Amazon's SQS functionality. This library provides a simple interface to interact with SQS's. A basic put, get and remove API with options to configure the SQS behavior.

##Installation

  $ npm install simple-sqs-queue

##Getting started

The easiest way to get started is to configure an SQS queue in AWS and use that url to configure a Queue. Specify the send and receive url, they can be the same queue. The region specifies the region the queue(s) are located in. The AWS access key and secret are needed to make requests to the SQS queues on your behalf. Create your keys through the IAM panel in the AWS console.

So far I have only been able to create queues in the same region. If there's a need for cross-region queue support, file an issue and I will take a look.

var aliceQueue = new Queue({
    urlQueueSend:       'http://url.to.your.send.queue',
    urlQueueReceive:    'http://url.to.your.receive.queue',
    awsRegion:          'queue-region'
    awsAccessKeyId:     '123456ABCDEF'
    awsSecretAccessKey: '098765HGFEDC'
  });

Sending a message to the queue is as easy as calling the put method on the queue. The payload has to be a string, but you can use any object you want and stringify it.

var payload = {
  id: 1234,
  type: SOME_TYPE
};

aliceQueue.put(JSON.stringify(payload), function(err, result) {
  // handle error if present
  // result contains a message id and response metadata
});

Get messages from the queue by calling get.

aliceQueue.get(function(err, result) {

  if (err || !result || result.length === 0) {
    // if error or no messages, there's nothing to do
    return;
  }

  // read the messages
  for (var key in result) {
    var message = result[key];
    var body = JSON.parse(message.Body);  // this is the actual payload that was send with the `put` method
    var id = body.id;

    // do some thingg
  }

});

SQS sometimes gives you messages twice. The library takes care of receiving messages twice and will only give you the ones that you haven't seen already, but this only works correctly if you call remove for the message after you've processed it. The library doesn't make the assumption that you've seen the message, only if you explicitly call remove for that message.

By calling remove the message is deleted from the queue, otherwise it will stay in the queue and keep showing up in subsequent get calls.

// pass in the message object that was in the array of results received by `get`
aliceQueue.remove(message, function(err, result) {
  // handle error or look at result
  // result contains a request id and response metadata
});

##Configuring

You have multiple options to configure a Queue. This library uses nconf to allow transparent use of environment variables and a config file. The following variables are used:

  • URL_QUEUE_SEND : url to the location of the SQS queue used for sending messages

  • URL_QUEUE_RECEIVE : url to the location of the SQS queue used for receiving messages

  • AWS_ACCESS_KEY_ID : AWS access key

  • AWS_SECRET_ACCESS_KEY : AWS secret key

  • AWS_REGION : region the queue's reside in

  • QUEUE_CONFIG : path to config.json that the Queue can optionaly use to configure itself

###Environment variables

Set the variables above to your environment: e.g. export AWS_SECRET_ACCESS_KEY=123467890abcdefgh

###config.json

Create a config.json. By default ./simple-sqs-queue-config.json is used, but you can override the path by setting the QUEUE_CONFIG env variable.

The config.json can also be passed in on instantiation in the options parameter. More on the options parameter below

var Queue     = require('simple-sqs-queue');
var bobQueue  = new Queue({
  configPath: './path/to/config.json'
});

###options parameter When creating a Queue, you can pass in an options parameter which will override environment variables and config file settings. The options keys have the same names as their config variables, but they are camelCased.

e.g.

var aliceQueue = new Queue({
  urlQueueSend:     URL_QUEUE_SEND,
  urlQueueReceive:  URL_QUEUE_RECEIVE,
  awsRegion:        AWS_REGION
});

##Running tests

  1. Clone the repository

  2. cd into the repository and install dependencies with npm install

  3. configure test.js with your Amazon SQS queue url's and region (TODO: pick configuration up from env variable or config)

  4. npm test