-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproducer.js
37 lines (31 loc) · 864 Bytes
/
producer.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
const {Kafka} = require("kafkajs")
const args = process.argv.slice(2)
const brokers = [args[0] + ":9092"]
const topic = args[1]
const clientId = "nodejs-producer"
const kafka = new Kafka({clientId, brokers})
const producer = kafka.producer()
const produce = async () => {
await producer.connect()
let i = 0
setInterval(async () => {
try {
await producer.send({
topic,
messages: [
{
key: String(i),
value: "this is message " + i,
},
],
})
console.log("writes: ", i)
i++
} catch (err) {
console.error("could not write message " + err)
}
}, 1000)
}
produce().catch((err) => {
console.error("error in producer: ", err)
})