1 | # AMQP:Hutch
|
2 | [amqplib](https://www.npmjs.com/package/amqplib) wrapper for easy setup and initialization.
|
3 |
|
4 | ## Setup
|
5 | Configuration and Events for AMQP Hutch.
|
6 | ```javascript
|
7 | var AMQPHutch = require('amqp-hutch');
|
8 |
|
9 | var hutch = new AMQPHutch();
|
10 |
|
11 | hutch.initialise({
|
12 | connectionString: 'amqps://user:password@host:port/uri?heartbeat=3',
|
13 | retryWait: 1000
|
14 | });
|
15 |
|
16 | hutch.on('ready', function() {
|
17 | console.log('Established RabbitMQ connection');
|
18 | });
|
19 |
|
20 | hutch.on('close', function(err) {
|
21 | console.log(err.message + 'RabbitMQ closing connection');
|
22 | });
|
23 |
|
24 | hutch.on('error', function(err) {
|
25 | console.log(err.message + 'RabbitMQ connection error');
|
26 | });
|
27 |
|
28 | module.exports = hutch;
|
29 | ```
|
30 |
|
31 | ## Publish
|
32 | ```javascript
|
33 | var message = {"Message": "Hello"};
|
34 |
|
35 | var 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 |
|
51 | hutch.publish(options, message, function(err, res) {
|
52 | console.log(res);
|
53 | });
|
54 | ```
|
55 |
|
56 |
|
57 | ## Publish to Exchange
|
58 | ```javascript
|
59 | var message = {"Message": "Hello"};
|
60 |
|
61 | var 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 |
|
75 | hutch.publishToExchange('exchange.name', 'topic', publishOptions, message, function(err, res) {
|
76 | console.log(res);
|
77 | });
|
78 | ```
|
79 |
|
80 | ## Consume
|
81 | Consume 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
|
110 | Adding 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
|
128 | Adding 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
|
146 | Destroy 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 |
|
158 | amqp-hutch offers two properties which may come in helpful for monitoring.
|
159 |
|
160 | ### status
|
161 | The status property can contain two values CONNECTED or DISCONNECTED.
|
162 |
|
163 | ### configuration
|
164 | The 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
|
174 | Errors triggered from the underlinging service calls with RabbitMQ can be caught by listening for events.
|
175 |
|
176 | ```javascript
|
177 | hutch.on('error', function(err) {
|
178 | console.log(err);
|
179 | });
|
180 | ```
|
181 |
|
182 | If the service is invoked prior to a connection been established the service will return a 'AMQPConnectionError'
|