-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Rick Harrison
committed
Jul 20, 2016
1 parent
933ccbf
commit 0c88498
Showing
2 changed files
with
71 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# @meadow/sqs | ||
|
||
[](https://circleci.com/gh/meadow/sqs) | ||
[](https://codecov.io/github/meadow/sqs?branch=master) | ||
[](https://www.npmjs.com/package/@meadow/sqs) | ||
|
||
Promise/JSON based client for Amazon SQS with automatic long-polling. | ||
|
||
- Requires node v6.x | ||
|
||
## Installation | ||
|
||
```js | ||
npm install @meadow/sqs --save | ||
``` | ||
|
||
## Usage | ||
|
||
Create a client: | ||
|
||
```js | ||
const sqs = require('@meadow/sqs'); | ||
|
||
const client = new sqs({ | ||
region: 'us-west-2', | ||
accessKeyId: 'AKFOOBAR', | ||
secretAccessKey: 'barbaz', | ||
queue: 'https://sqs.us-west-2.amazonaws.com/foobar/barbaz' | ||
}); | ||
``` | ||
|
||
Send a message: | ||
|
||
```js | ||
client.sendMessage({ | ||
type: 'ACTION_TYPE', | ||
payload: { | ||
foo: 'bar' | ||
} | ||
}); | ||
``` | ||
|
||
Long poll the queue for messages: | ||
|
||
```js | ||
client.pollQueue({ VisibilityTimeout: 600 }, function (message) { | ||
console.log(message); | ||
|
||
return new Promise(function (resolve) { | ||
// Perform the work for this message | ||
|
||
resolve(); | ||
}); | ||
}); | ||
``` | ||
|
||
## API | ||
|
||
Everything is built around promises and JSON. Amazon SQS only supports message bodies as strings, but this client will transform that to and from JSON for you automatically. When polling the queue, `@meadow/sqs` expects that you return a promise. When that promise resolves, the message will automatically be deleted from the SQS queue. | ||
|
||
#### sendMessage(payload) | ||
|
||
- `payload` - A JSON object that will be persisted to the Amazon SQS queue. | ||
|
||
#### pollQueue (options, handler) | ||
|
||
Calling this method will long-poll the Amazon SQS queue waiting for messages to come in. For each message that is received, the handler will be called with the body of the message as a JSON object. You are required to return a promise from your handler that resolves when the work for this message is completed. After the promise resolves, `@meadow/sqs` will automatically delete the message from the SQS queue. | ||
|
||
- `options` - These options will be sent to the Amazon SQS `receiveMessage` function defined here: http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/SQS.html#receiveMessage-property | ||
- `handler` - A function that returns a promise, which is meant to handle the message. The first parameter will be the message body as a JSON object. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters