-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsumer.js
More file actions
37 lines (33 loc) · 1.02 KB
/
Copy pathconsumer.js
File metadata and controls
37 lines (33 loc) · 1.02 KB
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").Kafka
// const { Kafka } = require('kafkajs')
console.log("Kafka consumer")
run();
async function run() {
try {
const kafka = new Kafka({
"clientId": "myapp",
"brokers": ["localhost:9092"]
})
const consumer = kafka.consumer({ "groupId": "test" });
console.log("Connecting...")
await consumer.connect()
console.log("Connected!")
await consumer.subscribe({
"topic": "Users",
"fromBeginning": true
})
await consumer.run({
"eachMessage": async result => {
console.log(`Received message: ${result.message.value} on partition: ${result.partition}`);
if (result.message.value == "quit") {
console.log("Consumer STOPPED!")
consumer.disconnect();
}
}
})
} catch (ex) {
console.error(`Error occurred ${ex}`)
} finally {
// process.exit(0);
}
}