-
Notifications
You must be signed in to change notification settings - Fork 1
/
publish.js
55 lines (45 loc) · 1.35 KB
/
publish.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
const amqplib = require('amqplib')
const amqpUrl = process.env.AMQP_URL || 'amqp://localhost'
async function do_publish() {
const conn = await amqplib.connect(amqpUrl);
const ch = await conn.createChannel()
const exch = 'ecommerce';
const queues = [
{
name: 'event',
rkey: 'event.#'
},
{
name: 'event-addtocart',
rkey: 'event.addtocart'
},
{
name: 'event-checkout',
rkey: 'event.checkout'
},
{
name: 'event-view',
rkey: 'event.view'
}
]
console.log('Creating Exchange')
await ch.assertExchange(exch, 'topic', { durable: true })
console.log('Creating & Binding Queue')
for (const item of queues) {
await ch.assertQueue(item.name, { durable: true });
await ch.bindQueue(item.name, exch, item.rkey);
}
const events = ['addtocart', 'checkout', 'view']
setInterval(async function () {
let event = events[Math.floor(Math.random() * events.length)];
let rkey = `event.${event}`
let msg = {
event,
rkey,
ts: new Date()
}
await ch.publish(exch, rkey, Buffer.from(JSON.stringify(msg)));
console.log(`Publish message = ${JSON.stringify(msg)}`)
}, 2000);
}
do_publish()