ys

ya-sqs

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

Showing:

Popularity

Downloads/wk

136

GitHub Stars

12

Maintenance

Last Commit

7yrs ago

Contributors

1

Package

Dependencies

3

Size (min+gzip)

373.5KB

License

MIT

Type Definitions

Tree-Shakeable

No?

Categories

Readme

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

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100
No reviews found
Be the first to rate

Alternatives

No alternatives found

Tutorials

No tutorials found
Add a tutorial