-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample-amqp-listener.js
72 lines (58 loc) · 1.96 KB
/
example-amqp-listener.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/******************************************************************************
*
* Example AMQP listener
*
* Defaults to read from environment (and .env), making it suitable for
* local confirmation and inspection of emmited messages.
*
*/
require('dotenv').config()
const amqp = require('amqplib')
const path = require('path')
const listen = async ({uri, exchange}) => {
console.log(`connecting to ${uri}...`)
const connection = await amqp.connect(uri)
console.log('creating channel...')
const channel = await connection.createChannel()
console.log(`asserting durable topic exchange ${exchange}...`)
await channel.assertExchange(exchange, 'topic', {durable: true})
console.log('asserting transient queue...')
const queue = await channel.assertQueue('', {exclusive: true})
console.log(`got queue ${queue.queue}`)
console.log('binding queue...')
channel.bindQueue(queue.queue, exchange, '#');
console.log('waiting for messages. Ctrl-C to exit...')
while (true) {
await channel.consume(queue.queue, msg =>
console.log(`got message with routingkey = ${msg?.fields?.routingKey}\r\n${msg.content.toString()}\r\n`),
{noAck: true})
}
}
const showHelp = () => {
console.log(
`
node ${path.basename(__filename)} --uri <amqp uri> --exchange <amqp exchange>
--uri defaults to AMPQP_URI
--exchange defaults to AMPQP_EXCHANGE
--help show this help
Environment is loaded from .env on start.
`)
}
const getNamedArg = name => process.argv
.map((v, i, a) => ({first: v, second: a[i+1]}))
.filter(({first}) => first === name).map(({second}) => second)
[0] || null
const hasArg = name => process.argv.includes(name)
const main = async () => {
const uri = getNamedArg('--uri') || process.env.AMQP_URI
const exchange = getNamedArg('--exchange') || process.env.AMQP_EXCHANGE
if (hasArg('--help')) {
return showHelp()
}
if (uri && exchange) {
return listen({uri, exchange})
}
return showHelp()
}
main()
.catch(err => console.error(err.stack))