Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/wookieb/alpha-amqp-consumer
Reliable message consumption via AMQP protocol
https://github.com/wookieb/alpha-amqp-consumer
alpha-packages amqp consumer nodejs
Last synced: about 1 month ago
JSON representation
Reliable message consumption via AMQP protocol
- Host: GitHub
- URL: https://github.com/wookieb/alpha-amqp-consumer
- Owner: wookieb
- License: mit
- Created: 2017-04-22T21:15:14.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2022-03-27T15:15:39.000Z (over 2 years ago)
- Last Synced: 2024-10-10T18:14:23.169Z (2 months ago)
- Topics: alpha-packages, amqp, consumer, nodejs
- Language: TypeScript
- Size: 202 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Alpha AMQP Consumer
[![CircleCI](https://circleci.com/gh/wookieb/alpha-amqp-consumer.svg?style=svg)](https://circleci.com/gh/wookieb/alpha-amqp-consumer)
Library for reliable message consumption via AMQP protocol.
Features:
* Automatically reconnects and restores channels for consumers
* Super easy to use
* Ability to stop/resume consumption
* Supports queue <-> exchange binding
* Supports queue assertion
* Allows to retry message consumption after certain amount of time
* Maintains counter of ongoing consumptions which helps implement [graceful app shutdown](examples/graceful-shutdown.js)
* Easy debugging with [debug](https://www.npmjs.com/package/debug) module## Install
```bash
npm install alpha-amqp-consumer
```## Usage
```javascript
const connect = require('alpha-amqp-consumer');connect('amqp://localhost')
.then(manager => {
manager.consume(
(message) => {
// once function execution is done the message will be ACK-ed
},
{queue: 'example-queue'},
);
});```
Usage with promises
```javascript// (...)
manager.consume(
{queue: 'example-queue'},
() => {
// automatically ACK-ed once promise gets resolved (in that case after 1 second)
return new Promise((resolve) => setTimeout(resolve, 1000));
}
);
// (...)```
## API
See special [API declaration file](docs/api.md) and [examples directory](./examples).## Message ACK-ing and REJECT-ing
Every consumer has "resultHandler" which a function that decides what to do with the messages based on the result from consumer function.
Message is rejected automatically it consumer function throws an error or returned promise gets rejected, otherwise message is ACKed.
You can customize the behavior by providing resultHandler```javascript
manager.consume((message) => {
// do something
}, {
queue: 'some-queue',
resultHandler(context, error) {
if (error) {
// maybe thrown error is fine?
if (isAcceptableError(error)) {
context.ack();
} else {
context.reject();
}
} else {
context.ack();
}
}
})
```## Retry with delay
There is no way to say AMQP to retry message consumption after certain period of time. In order to achieve that we need a bit more typology setup.We need:
- name of "pre" retry exchange
- name of "post-retry" exchange
- name of queue for temporary messages storageFor more information how retry works [another document](docs/how-retry-works.md).
```javascript
manager.setupDelayedRetryTopology({
exchange: {
pre: 'pre-retry',
post: 'post-retry'
},
queue: 'messages-to-retry'
})
.then(() => {
manager.consume(() => {
// do something with message
}, {
queue: 'some-queue',
resultHandler(context, error) {
if (error) {
context.retry(5000);
} else {
context.ack();
}
}
})
});
```## Debugging
Run your app with DEBUG env variable. See [debug package docs](https://www.npmjs.com/package/debug) for more details.
```bash
DEBUG=alpha-amqp-consumer:* node app.js
```# Changelog
## 0.4.3
* Added extra exported types## 0.4.2
* "channel" and "retryTopology" for ConsumerManager are now public
* "ConnectionManagerOptions" interface exported