Tortoise
npm install tortoise
A client library for interacting with AMQP.
- Basic Example
- Basic Setup
- Advanced Setup
- Publishing to a queue
- Publishing to an exchange
- Subscribing to a queue
- Accessing message data
- Handling Errors and Events
- Auto retrying and throttling
- Automatic setup of dead letter exchange and queue
- Configuring without the need to subscribe or publish
- Handling connection or channel closure
Basic Example
var Tortoise = tortoise = 'amqp://localhost'; tortoise ; ;
Basic Setup
var Tortoise = ;var tortoise = 'amqp://localhost';
Advanced Setup
var Tortoise = ;var options = connectRetries: -1 connectRetryInterval: 1000;var tortoise = 'amqp://localhost' options;
options
is optional. Current options are:
connectRetries
:Number
value greater than or equal to-1
. Defaults to-1
. Tortoise will attempt to connect up to this number. When set to-1
, tortoise will attempt to connect forever. Note: This does not handle connections that have already been established and were lost see Handling connection or channel closure for more information on that.connectRetryInterval
:Number
value greater than or equal to0
. Defaults to1000
. This is the amount of time, inms
, that tortoise will wait before attempting to connect again.
Publishing to a queue
tortoise ;
Publishing to an exchange
tortoise ;
Subscribing to a queue
tortoise ;
tortoise // Add as many bindings as needed ;
Automatically parsing JSON
There is an optional function setting that will automatically attempt to parse messages from JSON (using JSON.parse
) and if invalid, will nack(requeue=false)
the message. To capture this event each time it occurs, you can subscribe to your tortoise instance for event Tortoise.EVENTS.PARSEERROR
:
var Tortoise = ;var tortoise = 'amqp://localhost'; tortoise ; tortoise;
Accessing message data
The callback function provided to the subscribe
method will be scoped to the message, i.e. the this
object will contain the properties of the message. The object would look similar to this:
fields: deliveryTag: <int> redelivered: <bool> routingKey: <string> ... properties: contentType: <string> headers: ... ...
So, if I wanted to access the routingKey
that was provided, I would access it by:
tortoise ;
This is useful if you subcribe to wildcard topics on an exchange but wanted to know what the actual topic (routingKey
) was.
Handling Errors and Events
Tortoise will emit events when certain things occur. The following events are emitted:
PARSEERROR: 'TORTOISE.PARSEERROR' CONNECTIONCLOSED: 'TORTOISE.CONNECTIONCLOSED' CONNECTIONDISCONNECTED: 'TORTOISE.CONNECTIONDISCONNECTED'
These event strings are accessed by the EVENTS
property on the Tortoise
library, and can be subscribed to on an individual tortoise instance. Here is an example of being notified when a parse error occurred:
var Tortoise = ;var tortoise = 'amqp://localhost';// Do your tortoise configuration tortoise;
Auto retrying and throttling
There are a few methods available for controlling continuous failures, all are optional. failSpan
and retryTimeout
do nothing if failThreshold
is not set
default behavior (not setting) of failThreshold
is no failure handling
var Tortoise = tortoise = 'amqp://localhost'; tortoise // 3 immediate attempts // 10 minutes, defaults to 1 minute // 10 second timeout on each retry, defaults to 5 seconds ;
Automatic setup of dead letter exchange and queue
If you wanted to setup your (subscribe) queue to automatically set a dead letter exchange:
var Tortoise = tortoise = 'amqp://localhost'; tortoise ;
Declaring the queue to bind to the exchange is optional. It is perfectly acceptable to setup like this:
var Tortoise = tortoise = 'amqp://localhost'; tortoise ;
Configuring without the need to subscribe or publish
The .setup
method will call all asserts and bindings then close the channel
tortoise ; tortoise ;
Handling connection or channel closure
Automatic Method
There exists a helper method, .reestablish()
, to re-establish connections that were lost (when subscribing). It will attempt re-establish the connection and, when successful, will be configured with the same settings as before (queue, exchanges, etc). One caveat with this method is the .then()
resolution from the .subscribe()
method will no longer function after the connection is lost. In most cases that is not a problem.
It should be noted that this will begin consuming the connectRetries
limit. See Advanced Setup for more information.
Here is an example:
var Tortoise = ;var tortoise = 'amqp://localhost' connectRetries: -1 ; tortoise ;
If you would still like to know (for logging, etc) when a connection is closed, see the Handling Errors and Events section for subscribing to connection events.
Manual Method
When subscribing, the promise returned from .subscribe()
resolves with a channel object that can be listened on.
The following is an example of listening for close
events and resubscribing.
// Wrap subscription inside functionvar { tortoise } // Start subscribing;