Skip to content
This repository has been archived by the owner on Aug 24, 2019. It is now read-only.

gregberge/ya-sqs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ya-sqs

Build Status Dependency Status devDependency Status

Yet Another AWS SQS wrapper with pull (long polling), push, error management and promises.

Install

npm install ya-sqs

Usage

var sqs = require('ya-sqs');

var queue = sqs.createQueue({
  aws: {
    region: 'eu-west-1',
    accessKeyId: '...',
    secretAccessKey: '...'
  },
  name: 'ya-sqs-test'
});

// Push message in the queue.
queue.push({foo: 'bar'}, function (err) {
  console.log('Message pushed.');
});

// Pull message.
queue.pull(function (message, next) {
  console.log('Message pulled.');
  next(); // Remove message from queue and pull next.
});

Example with promises

var sqs = require('ya-sqs');

var queue = sqs.createQueue({
  aws: {region: 'eu-west-1'},
  name: 'ya-sqs-test'
});

// Push message in the queue.
queue.push({foo: 'bar'}).then(function () {
  console.log('Message pushed.');
});

// Pull message.
queue.pull(function (message) {
  console.log('Message pulled.');
  return Promise.resolve(); // Remove message from queue and pull next.
});

sqs.createQueue(options)

Create a new queue. If you provide a name, the queue will be automatically created.

{object} options
{string} [options.name] Name of the queue
{string} [options.url] Url of the queue
{string} [options.aws] AWS config
{string} [options.waitTime=20] Polling time
{string} [options.formatter] Formatter (default JSON)
var queue = sqs.createQueue({
  url: 'https://sqs.eu-west-1.amazonaws.com/279100839409/ya-sqs-test',
  waitTime: 10,
  aws: {
    region: 'eu-west',
    sslEnabled: true
  }
});

queue.push(message, [cb])

Push a new message in the queue.

Promises:

queue.push('hello').then(function () {
  console.log('Message pushed.');
}, function (err) {
  console.log('Error during push.');
});

Callback:

queue.push('hello', function (err) {
  if (err) return console.log('Error during push.');
  console.log('Message pushed.');
});

queue.mpush(messages, [cb])

Push message using batch method.

Promises:

queue.mpush(['hello', 'world']).then(function () {
  console.log('Messages pushed.');
}, function (err) {
  console.log('Error during push.');
});

Callback:

queue.mpush(['hello', 'world'], function (err) {
  if (err) return console.log('Error during push.');
  console.log('Message pushed.');
});

queue.pull(handler, [options])

Pull message from the queue. When the promise returned is resolved or when next is called, the message will be remove and an other message will be pulled. If an error is sent, the error will be emitted and the message will not be removed.

Options:

@param {object} [options]
@param {number} [options.maxNumberOfMessages=10] Max number of messages

Promises:

queue.pull(function (message) {
  console.log('Message pulled.', message);
  return Promise.resolve();
});

Callback:

queue.pull(function (message, next) {
  console.log('Message pulled.', message);
  next();
});

queue.close([cb])

Stop pulling events, the promise is resolved when current messages have been processed.

queue.close().then(...);

Formatters

The default formatter for the queue is JSON, you can write a custom formatter for messages. To do it, please refer to the JSON formatter.

Example that specify a raw formatter (the message will not be formatted):

ws.createQueue({
  formatter: {
    format: function (message) {
      return Promise.resolve(message);
    },
    parse: function (message) {
      return Promise.resolve(message);  
    }
  }
})

Events

"error"

Emitted when ReceiveMessage command has an error, when a message can't be parsed and when you return an error in the "next" method.

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

"message pushed"

Emitted when a message is pushed. The message in argument is not formatted.

queue.on('message pushed', function (message) {
  // ...
});

"message received"

Emitted when a new message is received. The message in argument is not parsed.

queue.on('message received', function (message) {
  // ...
});

"message processed"

Emitted when a message is processed. The message in argument is not parsed.

queue.on('message processed', function (message) {
  // ...
});

License

MIT

About

Yet Another AWS SQS wrapper with pull (long polling), push, error management and promises.

Resources

Stars

Watchers

Forks

Packages

No packages published