amqplib 0.10.3 → 0.10.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. package/.github/ISSUE_TEMPLATE/bug_report.md +7 -4
  2. package/.github/workflows/publish.yml +59 -0
  3. package/.github/workflows/test.yml +15 -2
  4. package/CHANGELOG.md +14 -1
  5. package/Makefile +22 -20
  6. package/README.md +1 -2
  7. package/bin/amqp-rabbitmq-0.9.1.json +483 -0
  8. package/bin/generate-defs.js +5 -5
  9. package/examples/direct_reply_to_client.js +22 -0
  10. package/examples/direct_reply_to_server.js +25 -0
  11. package/examples/headers.js +29 -31
  12. package/examples/ssl.js +16 -10
  13. package/examples/stream_queues/README.md +40 -0
  14. package/examples/stream_queues/package.json +14 -0
  15. package/examples/stream_queues/receive_stream.js +55 -0
  16. package/examples/stream_queues/send_stream.js +38 -0
  17. package/examples/tutorials/callback_api/emit_log.js +22 -22
  18. package/examples/tutorials/callback_api/emit_log_direct.js +24 -24
  19. package/examples/tutorials/callback_api/emit_log_topic.js +23 -20
  20. package/examples/tutorials/callback_api/new_task.js +21 -20
  21. package/examples/tutorials/callback_api/receive.js +27 -21
  22. package/examples/tutorials/callback_api/receive_logs.js +32 -26
  23. package/examples/tutorials/callback_api/receive_logs_direct.js +42 -41
  24. package/examples/tutorials/callback_api/receive_logs_topic.js +43 -40
  25. package/examples/tutorials/callback_api/rpc_client.js +39 -37
  26. package/examples/tutorials/callback_api/rpc_server.js +42 -32
  27. package/examples/tutorials/callback_api/send.js +21 -22
  28. package/examples/tutorials/callback_api/worker.js +29 -25
  29. package/examples/tutorials/emit_log.js +20 -15
  30. package/examples/tutorials/emit_log_direct.js +22 -16
  31. package/examples/tutorials/emit_log_topic.js +22 -15
  32. package/examples/tutorials/new_task.js +20 -13
  33. package/examples/tutorials/receive.js +19 -14
  34. package/examples/tutorials/receive_logs.js +25 -23
  35. package/examples/tutorials/receive_logs_direct.js +29 -35
  36. package/examples/tutorials/receive_logs_topic.js +28 -32
  37. package/examples/tutorials/rpc_client.js +37 -40
  38. package/examples/tutorials/rpc_server.js +36 -29
  39. package/examples/tutorials/send.js +26 -18
  40. package/examples/tutorials/worker.js +27 -21
  41. package/examples/waitForConfirms.js +18 -19
  42. package/lib/callback_model.js +295 -290
  43. package/lib/channel.js +353 -346
  44. package/lib/channel_model.js +5 -1
  45. package/lib/codec.js +26 -2
  46. package/lib/connection.js +544 -532
  47. package/lib/credentials.js +1 -1
  48. package/lib/defs.js +2568 -2457
  49. package/lib/format.js +0 -1
  50. package/lib/heartbeat.js +32 -29
  51. package/lib/mux.js +96 -99
  52. package/package.json +3 -5
  53. package/test/callback_api.js +182 -174
  54. package/test/channel_api.js +455 -445
  55. package/test/codec.js +15 -15
  56. package/test/data.js +15 -0
  57. package/test/frame.js +6 -7
  58. package/test/mux.js +1 -2
  59. package/test/util.js +1 -3
@@ -1,25 +1,20 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- // Example of using a headers exchange
4
-
5
- var amqp = require('../')
6
-
7
- amqp.connect().then(function(conn) {
8
- return conn.createChannel().then(withChannel);
9
- }, console.error);
10
-
11
- function withChannel(ch) {
12
- // NB the type of the exchange is 'headers'
13
- ch.assertExchange('matching exchange', 'headers').then(function(ex) {
14
- ch.assertQueue().then(function(q) {
15
- bindAndConsume(ch, ex, q).then(function() {
16
- send(ch, ex);
17
- });
18
- });
3
+ const amqp = require('../');
4
+
5
+ (async () => {
6
+
7
+ const connection = await amqp.connect();
8
+ const channel = await connection.createChannel();
9
+
10
+ process.once('SIGINT', async () => {
11
+ await channel.close();
12
+ await connection.close();
19
13
  });
20
- }
21
14
 
22
- function bindAndConsume(ch, ex, q) {
15
+ const { exchange } = await channel.assertExchange('matching exchange', 'headers');
16
+ const { queue } = await channel.assertQueue();
17
+
23
18
  // When using a headers exchange, the headers to be matched go in
24
19
  // the binding arguments. The routing key is ignore, so best left
25
20
  // empty.
@@ -27,16 +22,19 @@ function bindAndConsume(ch, ex, q) {
27
22
  // 'x-match' is 'all' or 'any', meaning "all fields must match" or
28
23
  // "at least one field must match", respectively. The values to be
29
24
  // matched go in subsequent fields.
30
- ch.bindQueue(q.queue, ex.exchange, '', {'x-match': 'any',
31
- 'foo': 'bar',
32
- 'baz': 'boo'});
33
- return ch.consume(q.queue, function(msg) {
34
- console.log(msg.content.toString());
35
- }, {noAck: true});
36
- }
37
-
38
- function send(ch, ex) {
39
- // The headers for a message are given as an option to `publish`:
40
- ch.publish(ex.exchange, '', Buffer.from('hello'), {headers: {baz: 'boo'}});
41
- ch.publish(ex.exchange, '', Buffer.from('world'), {headers: {foo: 'bar'}});
42
- }
25
+ await channel.bindQueue(queue, exchange, '', {
26
+ 'x-match': 'any',
27
+ 'foo': 'bar',
28
+ 'baz': 'boo'
29
+ });
30
+
31
+ await channel.consume(queue, (message) => {
32
+ console.log(message.content.toString());
33
+ }, { noAck: true });
34
+
35
+ channel.publish(exchange, '', Buffer.from('hello'), { headers: { baz: 'boo' }});
36
+ channel.publish(exchange, '', Buffer.from('hello'), { headers: { foo: 'bar' }});
37
+ channel.publish(exchange, '', Buffer.from('lost'), { headers: { meh: 'nah' }});
38
+
39
+ console.log(' [x] To exit press CTRL+C.');
40
+ })();
package/examples/ssl.js CHANGED
@@ -18,8 +18,8 @@
18
18
  //
19
19
  // openssl s_client -connect localhost:5671
20
20
 
21
- var amqp = require('../');
22
- var fs = require('fs');
21
+ const amqp = require('../');
22
+ const fs = require('fs');
23
23
 
24
24
  // Assemble the SSL options; for verification we need at least
25
25
  // * a certificate to present to the server ('cert', in PEM format)
@@ -34,7 +34,7 @@ var fs = require('fs');
34
34
  // to use `rejectUnauthorized: false`.
35
35
 
36
36
  // Options for full client and server verification:
37
- var opts = {
37
+ const opts = {
38
38
  cert: fs.readFileSync('../etc/client/cert.pem'),
39
39
  key: fs.readFileSync('../etc/client/key.pem'),
40
40
  // cert and key or
@@ -49,16 +49,22 @@ var opts = {
49
49
  // {verify, verify_none},
50
50
  // {fail_if_no_peer_cert,false}
51
51
  //
52
- // var opts = { ca: [fs.readFileSync('../etc/testca/cacert.pem')] };
52
+ // const opts = { ca: [fs.readFileSync('../etc/testca/cacert.pem')] };
53
53
 
54
54
  // Option to use the SSL client certificate for authentication
55
55
  // opts.credentials = amqp.credentials.external();
56
56
 
57
- var open = amqp.connect('amqps://localhost', opts);
57
+ (async () => {
58
+ const connection = await amqp.connect('amqp://localhost', opts);
59
+ const channel = await connection.createChannel();
58
60
 
59
- open.then(function(conn) {
60
- process.on('SIGINT', conn.close.bind(conn));
61
- return conn.createChannel().then(function(ch) {
62
- ch.sendToQueue('foo', Buffer.from('Hello World!'));
61
+ process.on('SIGINT', async () => {
62
+ await channel.close();
63
+ await connection.close();
63
64
  });
64
- }).then(null, console.warn);
65
+
66
+ channel.sendToQueue('foo', Buffer.from('Hello World!'));
67
+
68
+ console.log(' [x] To exit press CTRL+C.');
69
+ })();
70
+
@@ -0,0 +1,40 @@
1
+ RabbitMQ Stream Examples
2
+ ---
3
+ The [stream queues](https://www.rabbitmq.com/streams.html) are available starting from RabbitMQ 3.9
4
+
5
+ These examples show how to use stream queues with the lib.
6
+
7
+ Send a message to a stream queue
8
+ ```
9
+ node send_stream.js
10
+ ```
11
+
12
+ Receive all the messages from stream queue:
13
+ ```
14
+ node receive_stream.js
15
+ ```
16
+
17
+ Consumers can be configured to receive messages using an offset via the `x-stream-offset` argument. e.g.
18
+
19
+ ```js
20
+ channel.consume(queue, onMessage, {
21
+ noAck: false,
22
+ arguments: {
23
+ 'x-stream-offset': 'first'
24
+ }
25
+ });
26
+ ```
27
+
28
+ RabbitMQ supports six different types of offset, however specifying them can be
29
+
30
+ | Offset Type | Example Value | Notes |
31
+ |-----------|----------------------------------------------------------|-------|
32
+ | First | `{'x-stream-offset':'first'}` | Start from the first message in the log |
33
+ | Last | `{'x-stream-offset':'last'}` | Start from the last "chunk" of messages (could be multiple messages) |
34
+ | Next | `{'x-stream-offset':'next'}` | Start from the next message (the default) |
35
+ | Offset | `{'x-stream-offset':5}` | a numerical value specifying an exact offset to attach to the log at |
36
+ | Timestamp | `{'x-stream-offset':{'!':'timestamp',value:1686519750}}` | a timestamp value specifying the point in time to attach to the log at. The timestamp must be the number of seconds since 00:00:00 UTC, 1970-01-01. Consumers can receive messages published a bit before the specified timestamp. |
37
+ | Interval | `{'x-stream-offset':'1h'}` | the time interval relative to current time to attach the log at. Valid units are Y, M, D, h, m and s |
38
+
39
+
40
+ See https://www.rabbitmq.com/streams.html#consuming for more details
@@ -0,0 +1,14 @@
1
+ {
2
+ "name": "stream_queues",
3
+ "version": "1.0.0",
4
+ "description": "An example demonstrating use of stream queues",
5
+ "main": "n",
6
+ "dependencies": {
7
+ "amqplib": "^0.10.3"
8
+ },
9
+ "devDependencies": {},
10
+ "scripts": {
11
+ "test": "echo \"Error: no test specified\" && exit 1"
12
+ },
13
+ "license": "ISC"
14
+ }
@@ -0,0 +1,55 @@
1
+ const amqp = require('amqplib');
2
+
3
+ (async () => {
4
+ try {
5
+ const connection = await amqp.connect('amqp://localhost');
6
+ process.once('SIGINT', connection.close);
7
+
8
+ const channel = await connection.createChannel();
9
+ const queue = 'my_first_stream';
10
+
11
+ // Define the queue stream
12
+ // Mandatory: exclusive: false, durable: true autoDelete: false
13
+ await channel.assertQueue(queue, {
14
+ exclusive: false,
15
+ durable: true,
16
+ autoDelete: false,
17
+ arguments: {
18
+ 'x-queue-type': 'stream', // Mandatory to define stream queue
19
+ 'x-max-length-bytes': 2_000_000_000 // Set the queue retention to 2GB else the stream doesn't have any limit
20
+ }
21
+ });
22
+
23
+ channel.qos(100); // This is mandatory
24
+
25
+ channel.consume(queue, (msg) => {
26
+ console.log(" [x] Received '%s'", msg.content.toString());
27
+ channel.ack(msg); // Mandatory
28
+ }, {
29
+ noAck: false,
30
+ arguments: {
31
+ /*
32
+ Here you can specify the offset: : first, last, next, offset, timestamp and interval, i.e.
33
+
34
+ 'x-stream-offset': 'first'
35
+ 'x-stream-offset': 'last'
36
+ 'x-stream-offset': 'next'
37
+ 'x-stream-offset': 5
38
+ 'x-stream-offset': { '!': 'timestamp', value: 1686519750 }
39
+ 'x-stream-offset': '1h'
40
+
41
+ The timestamp must be the desired number of seconds since 00:00:00 UTC, 1970-01-01
42
+ The interval units can be Y, M, D, h, m, s
43
+
44
+ */
45
+ 'x-stream-offset': 'first'
46
+ }
47
+ });
48
+
49
+ console.log(' [*] Waiting for messages. To exit press CTRL+C');
50
+ }
51
+ // Catch and display any errors in the console
52
+ catch(e) {
53
+ console.log(e)
54
+ }
55
+ })();
@@ -0,0 +1,38 @@
1
+ const amqp = require('amqplib');
2
+
3
+
4
+ (async () => {
5
+ try {
6
+ const connection = await amqp.connect('amqp://localhost');
7
+ process.once('SIGINT', connection.close);
8
+
9
+ const channel = await connection.createChannel();
10
+ const queue = 'my_first_stream';
11
+ const msg = `Hello World! ${Date.now()}`;
12
+
13
+ // Define the queue stream
14
+ // Mandatory: exclusive: false, durable: true autoDelete: false
15
+ await channel.assertQueue(queue, {
16
+ exclusive: false,
17
+ durable: true,
18
+ autoDelete: false,
19
+ arguments: {
20
+ 'x-queue-type': 'stream', // Mandatory to define stream queue
21
+ 'x-max-length-bytes': 2_000_000_000 // Set the queue retention to 2GB else the stream doesn't have any limit
22
+ }
23
+ });
24
+
25
+ // Send the message to the stream queue
26
+ await channel.sendToQueue(queue, Buffer.from(msg));
27
+ console.log(" [x] Sent '%s'", msg);
28
+ await channel.close();
29
+
30
+ // Close connection
31
+ connection.close();
32
+ }
33
+ // Catch and display any errors in the console
34
+ catch(e) {
35
+ console.log(e)
36
+ }
37
+ })();
38
+
@@ -1,28 +1,28 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- var amqp = require('amqplib/callback_api');
3
+ const amqp = require('amqplib/callback_api');
4
4
 
5
- function bail(err, conn) {
6
- console.error(err);
7
- if (conn) conn.close(function() { process.exit(1); });
8
- }
9
-
10
- function on_connect(err, conn) {
11
- if (err !== null) return bail(err);
5
+ const exchange = 'logs';
6
+ const text = process.argv.slice(2).join(' ') || 'info: Hello World!';
12
7
 
13
- var ex = 'logs';
8
+ amqp.connect((err, connection) => {
9
+ if (err) return bail(err);
10
+ connection.createChannel((err, channel) => {
11
+ if (err) return bail(err, connection);
12
+ channel.assertExchange(exchange, 'fanout', { durable: false }, (err) => {
13
+ if (err) return bail(err, connection);
14
+ channel.publish(exchange, '', Buffer.from(text));
15
+ console.log(" [x] Sent '%s'", text);
16
+ channel.close(() => {
17
+ connection.close();
18
+ });
19
+ });
20
+ });
21
+ });
14
22
 
15
- function on_channel_open(err, ch) {
16
- if (err !== null) return bail(err, conn);
17
- ch.assertExchange(ex, 'fanout', {durable: false});
18
- var msg = process.argv.slice(2).join(' ') ||
19
- 'info: Hello World!';
20
- ch.publish(ex, '', Buffer.from(msg));
21
- console.log(" [x] Sent '%s'", msg);
22
- ch.close(function() { conn.close(); });
23
- }
24
-
25
- conn.createChannel(on_channel_open);
23
+ function bail(err, connection) {
24
+ console.error(err);
25
+ if (connection) connection.close(() => {
26
+ process.exit(1);
27
+ });
26
28
  }
27
-
28
- amqp.connect(on_connect);
@@ -1,30 +1,30 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- var amqp = require('amqplib/callback_api');
3
+ const amqp = require('amqplib/callback_api');
4
4
 
5
- var args = process.argv.slice(2);
6
- var severity = (args.length > 0) ? args[0] : 'info';
7
- var message = args.slice(1).join(' ') || 'Hello World!';
5
+ const exchange = 'direct_logs';
6
+ const args = process.argv.slice(2);
7
+ const routingKey = (args.length > 0) ? args[0] : 'info';
8
+ const text = args.slice(1).join(' ') || 'Hello World!';
8
9
 
9
- function bail(err, conn) {
10
- console.error(err);
11
- if (conn) conn.close(function() { process.exit(1); });
12
- }
13
-
14
- function on_connect(err, conn) {
15
- if (err !== null) return bail(err);
16
-
17
- var ex = 'direct_logs';
18
- var exopts = {durable: false};
19
-
20
- function on_channel_open(err, ch) {
21
- if (err !== null) return bail(err, conn);
22
- ch.assertExchange(ex, 'direct', exopts, function(err, ok) {
23
- ch.publish(ex, severity, Buffer.from(message));
24
- ch.close(function() { conn.close(); });
10
+ amqp.connect((err, connection) => {
11
+ if (err) return bail(err);
12
+ connection.createChannel((err, channel) => {
13
+ if (err) return bail(err, connection);
14
+ channel.assertExchange(exchange, 'direct', { durable: false }, (err) => {
15
+ if (err) return bail(err, connection);
16
+ channel.publish(exchange, routingKey, Buffer.from(text));
17
+ console.log(" [x] Sent '%s'", text);
18
+ channel.close(() => {
19
+ connection.close();
20
+ });
25
21
  });
26
- }
27
- conn.createChannel(on_channel_open);
28
- }
22
+ });
23
+ });
29
24
 
30
- amqp.connect(on_connect);
25
+ function bail(err, connection) {
26
+ console.error(err);
27
+ if (connection) connection.close(() => {
28
+ process.exit(1);
29
+ });
30
+ }
@@ -1,27 +1,30 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- var amqp = require('amqplib/callback_api');
3
+ const amqp = require('amqplib/callback_api');
4
4
 
5
- var args = process.argv.slice(2);
6
- var key = (args.length > 0) ? args[0] : 'info';
7
- var message = args.slice(1).join(' ') || 'Hello World!';
5
+ const exchange = 'topic_logs';
6
+ const args = process.argv.slice(2);
7
+ const routingKey = (args.length > 0) ? args[0] : 'info';
8
+ const text = args.slice(1).join(' ') || 'Hello World!';
8
9
 
9
- function bail(err, conn) {
10
- console.error(err);
11
- if (conn) conn.close(function() { process.exit(1); });
12
- }
13
-
14
- function on_connect(err, conn) {
15
- if (err !== null) return bail(err);
16
- var ex = 'topic_logs', exopts = {durable: false};
17
- conn.createChannel(function(err, ch) {
18
- ch.assertExchange(ex, 'topic', exopts, function(err, ok) {
19
- if (err !== null) return bail(err, conn);
20
- ch.publish(ex, key, Buffer.from(message));
21
- console.log(" [x] Sent %s:'%s'", key, message);
22
- ch.close(function() { conn.close(); });
10
+ amqp.connect((err, connection) => {
11
+ if (err) return bail(err);
12
+ connection.createChannel((err, channel) => {
13
+ if (err) return bail(err, connection);
14
+ channel.assertExchange(exchange, 'topic', { durable: false }, (err) => {
15
+ if (err) return bail(err, connection);
16
+ channel.publish(exchange, routingKey, Buffer.from(text));
17
+ console.log(" [x] Sent '%s'", text);
18
+ channel.close(() => {
19
+ connection.close();
20
+ });
23
21
  });
24
22
  });
25
- }
23
+ });
26
24
 
27
- amqp.connect(on_connect);
25
+ function bail(err, connection) {
26
+ console.error(err);
27
+ if (connection) connection.close(() => {
28
+ process.exit(1);
29
+ });
30
+ }
@@ -1,27 +1,28 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- var amqp = require('amqplib/callback_api');
3
+ const amqp = require('amqplib/callback_api');
4
4
 
5
- function bail(err, conn) {
6
- console.error(err);
7
- if (conn) conn.close(function() { process.exit(1); });
8
- }
5
+ const queue = 'task_queue';
6
+ const text = process.argv.slice(2).join(' ') || "Hello World!";
9
7
 
10
- function on_connect(err, conn) {
11
- if (err !== null) return bail(err);
12
-
13
- var q = 'task_queue';
14
-
15
- conn.createChannel(function(err, ch) {
16
- if (err !== null) return bail(err, conn);
17
- ch.assertQueue(q, {durable: true}, function(err, _ok) {
18
- if (err !== null) return bail(err, conn);
19
- var msg = process.argv.slice(2).join(' ') || "Hello World!";
20
- ch.sendToQueue(q, Buffer.from(msg), {persistent: true});
21
- console.log(" [x] Sent '%s'", msg);
22
- ch.close(function() { conn.close(); });
8
+ amqp.connect((err, connection) => {
9
+ if (err) return bail(err);
10
+ connection.createChannel((err, channel) => {
11
+ if (err) return bail(err, connection);
12
+ channel.assertQueue(queue, { durable: true }, (err) => {
13
+ if (err) return bails(err, connection);
14
+ channel.sendToQueue(queue, Buffer.from(text), { persistent: true });
15
+ console.log(" [x] Sent '%s'", text);
16
+ channel.close(() => {
17
+ connection.close();
18
+ });
23
19
  });
24
20
  });
25
- }
21
+ });
26
22
 
27
- amqp.connect(on_connect);
23
+ function bail(err, connection) {
24
+ console.error(err);
25
+ if (connection) connection.close(() => {
26
+ process.exit(1);
27
+ });
28
+ }
@@ -1,30 +1,36 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- var amqp = require('amqplib/callback_api');
3
+ const amqp = require('amqplib/callback_api');
4
4
 
5
- function bail(err, conn) {
6
- console.error(err);
7
- if (conn) conn.close(function() { process.exit(1); });
8
- }
5
+ const queue = 'hello';
9
6
 
10
- function on_connect(err, conn) {
11
- if (err !== null) return bail(err);
12
- process.once('SIGINT', function() { conn.close(); });
13
-
14
- var q = 'hello';
7
+ amqp.connect((err, connection) => {
8
+ if (err) return bail(err);
9
+ connection.createChannel((err, channel) => {
10
+ if (err) return bail(err, connection);
15
11
 
16
- function on_channel_open(err, ch) {
17
- ch.assertQueue(q, {durable: false}, function(err, ok) {
18
- if (err !== null) return bail(err, conn);
19
- ch.consume(q, function(msg) { // message callback
20
- console.log(" [x] Received '%s'", msg.content.toString());
21
- }, {noAck: true}, function(_consumeOk) { // consume callback
22
- console.log(' [*] Waiting for messages. To exit press CTRL+C');
12
+ process.once('SIGINT', () => {
13
+ channel.close(() => {
14
+ connection.close();
23
15
  });
24
16
  });
25
- }
26
17
 
27
- conn.createChannel(on_channel_open);
28
- }
18
+ channel.assertQueue(queue, { durable: false }, (err) => {
19
+ if (err) return bail(err, connection);
20
+ channel.consume(queue, (message) => {
21
+ if (message) console.log(" [x] Received '%s'", message.content.toString());
22
+ else console.warn(' [x] Consumer cancelled');
23
+ }, { noAck: true }, (err) => {
24
+ if (err) return bail(err, connection);
25
+ console.log(" [*] Waiting for logs. To exit press CTRL+C.");
26
+ });
27
+ });
28
+ });
29
+ });
29
30
 
30
- amqp.connect(on_connect);
31
+ function bail(err, connection) {
32
+ console.error(err);
33
+ if (connection) connection.close(() => {
34
+ process.exit(1);
35
+ });
36
+ }
@@ -1,36 +1,42 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- var amqp = require('amqplib/callback_api');
3
+ const amqp = require('amqplib/callback_api');
4
4
 
5
- function bail(err, conn) {
6
- console.error(err);
7
- if (conn) conn.close(function() { process.exit(1); });
8
- }
5
+ const exchange = 'logs';
9
6
 
10
- function on_connect(err, conn) {
11
- if (err !== null) return bail(err);
12
- process.once('SIGINT', function() { conn.close(); });
7
+ amqp.connect((err, connection) => {
8
+ if (err) return bail(err);
9
+ connection.createChannel((err, channel) => {
10
+ if (err) return bail(err, connection);
13
11
 
14
- var ex = 'logs';
15
-
16
- function on_channel_open(err, ch) {
17
- if (err !== null) return bail(err, conn);
18
- ch.assertQueue('', {exclusive: true}, function(err, ok) {
19
- var q = ok.queue;
20
- ch.bindQueue(q, ex, '');
21
- ch.consume(q, logMessage, {noAck: true}, function(err, ok) {
22
- if (err !== null) return bail(err, conn);
23
- console.log(" [*] Waiting for logs. To exit press CTRL+C.");
12
+ process.once('SIGINT', () => {
13
+ channel.close(() => {
14
+ connection.close();
24
15
  });
25
16
  });
26
- }
27
17
 
28
- function logMessage(msg) {
29
- if (msg)
30
- console.log(" [x] '%s'", msg.content.toString());
31
- }
18
+ channel.assertExchange(exchange, 'fanout', { durable: false }, (err, { queue }) => {
19
+ if (err) return bail(err, connection);
20
+ channel.assertQueue('', { exclusive: true }, (err, { queue }) => {
21
+ if (err) return bail(err, connection);
22
+ channel.bindQueue(queue, exchange, '', {}, (err) => {
23
+ if (err) return bail(err, connection);
24
+ channel.consume(queue, (message) => {
25
+ if (message) console.log(" [x] '%s'", message.content.toString());
26
+ else console.warn(' [x] Consumer cancelled');
27
+ }, { noAck: true }, (err) => {
28
+ if (err) return bail(err, connection);
29
+ console.log(" [*] Waiting for logs. To exit press CTRL+C.");
30
+ });
31
+ });
32
+ });
33
+ });
34
+ });
35
+ });
32
36
 
33
- conn.createChannel(on_channel_open);
37
+ function bail(err, connection) {
38
+ console.error(err);
39
+ if (connection) connection.close(() => {
40
+ process.exit(1);
41
+ });
34
42
  }
35
-
36
- amqp.connect(on_connect);