UNPKG

3.85 kBMarkdownView Raw
1# AMQP:Hutch
2[amqplib](https://www.npmjs.com/package/amqplib) wrapper for easy setup and initialization.
3
4## Setup
5Configuration and Events for AMQP Hutch.
6```javascript
7var AMQPHutch = require('amqp-hutch');
8
9var hutch = new AMQPHutch();
10
11hutch.initialise({
12 connectionString: 'amqps://user:password@host:port/uri?heartbeat=3',
13 retryWait: 1000
14});
15
16hutch.on('ready', function() {
17 console.log('Established RabbitMQ connection');
18});
19
20hutch.on('close', function(err) {
21 console.log(err.message + 'RabbitMQ closing connection');
22});
23
24hutch.on('error', function(err) {
25 console.log(err.message + 'RabbitMQ connection error');
26});
27
28module.exports = hutch;
29```
30
31## Publish
32```javascript
33var message = {"Message": "Hello"};
34
35var options = {
36 exchange: {
37 durable: true,
38 confirm: true,
39 autoDelete: false,
40 type: 'topic',
41 name: 'example.exchange'
42 },
43 publish: {
44 persistent: true,
45 contentType: 'application/json',
46 expiration: 86400000,
47 timestamp: Math.floor(Date.now() / 1000)
48 }
49};
50
51hutch.publish(options, message, function(err, res) {
52 console.log(res);
53});
54```
55
56
57## Publish to Exchange
58```javascript
59var message = {"Message": "Hello"};
60
61var publishOptions = {
62 exchange: {
63 durable: true,
64 confirm: true,
65 autoDelete: false
66 },
67 publish: {
68 persistent: true,
69 contentType: 'application/json',
70 expiration: 86400000,
71 timestamp: Math.floor(Date.now() / 1000)
72 }
73};
74
75hutch.publishToExchange('exchange.name', 'topic', publishOptions, message, function(err, res) {
76 console.log(res);
77});
78```
79
80## Consume
81Consume creates a queue bound to a new channel.
82```javascript
83
84 var options = {
85 exchange: {
86 name: 'exchange.name',
87 type: 'topic'
88 },
89 queue: {
90 name: 'queue.name',
91 prefetch: 1,
92 durable: true
93 },
94 routingKey: '#'
95 };
96
97 var consumer = function(message, done, fail) {
98 some.service(message, function(err, res) {
99 if(err) return fail();
100 done();
101 });
102 };
103
104 hutch.consume(options, consumer, function(err) {
105 console.log("Successfully setup consumer for queue: [" + options.queue + "]");
106 });
107```
108
109### Exclusive
110Adding the Exclusive flag to the options will manage an exclusive consumer, the conusmer will retry until closed.
111```javascript
112
113 var options = {
114 exchange: {
115 name: 'exchange.name',
116 type: 'topic'
117 },
118 queue: {
119 name: 'queue.name',
120 prefetch: 1,
121 durable: true
122 },
123 exclusive: true
124 };
125```
126
127### SkipNext
128Adding the skipNext flag to the options will skip the next message in the queue before initialising, this can be useful for unblocking failed messages.
129```javascript
130
131 var options = {
132 exchange: {
133 name: 'exchange.name',
134 type: 'topic'
135 },
136 queue: {
137 name: 'queue.name',
138 prefetch: 1,
139 durable: true
140 },
141 skipNext: true
142 };
143```
144
145## Destroy
146Destroy will unbind/purge the queue from the given exchange.
147```javascript
148 var queue = "queue.name";
149 var exchange = "exchange.name";
150
151 hutch.destroy(queue, exchange, function(err) {
152 console.log("Successfully unbound queue: [" + queue + "]");
153 });
154 ```
155
156## Monitoring
157
158amqp-hutch offers two properties which may come in helpful for monitoring.
159
160### status
161The status property can contain two values CONNECTED or DISCONNECTED.
162
163### configuration
164The configuration object contains the original configuration passed to amqp-hutch.
165
166
167```javascript
168 if (hutch.isConnected()){
169 console.log(hutch.configuration.connectionString)
170 }
171 ```
172
173## Error Handing
174Errors triggered from the underlinging service calls with RabbitMQ can be caught by listening for events.
175
176```javascript
177hutch.on('error', function(err) {
178 console.log(err);
179});
180```
181
182If the service is invoked prior to a connection been established the service will return a 'AMQPConnectionError'