-
Notifications
You must be signed in to change notification settings - Fork 18
/
consumer.js
51 lines (48 loc) · 1.3 KB
/
consumer.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
var Kafka = require("node-rdkafka");
var kafkaConf = {
"group.id": "cloudkarafka-example",
"metadata.broker.list": process.env.CLOUDKARAFKA_BROKERS.split(","),
"socket.keepalive.enable": true,
"security.protocol": "SASL_SSL",
"sasl.mechanisms": "SCRAM-SHA-256",
"sasl.username": process.env.CLOUDKARAFKA_USERNAME,
"sasl.password": process.env.CLOUDKARAFKA_PASSWORD,
"debug": "generic,broker,security"
};
const prefix = process.env.CLOUDKARAFKA_USERNAME;
const topics = [`${prefix}.test`];
const consumer = new Kafka.KafkaConsumer(kafkaConf, {
"auto.offset.reset": "beginning"
});
const numMessages = 5;
let counter = 0;
consumer.on("error", function(err) {
console.error(err);
});
consumer.on("ready", function(arg) {
console.log(`Consumer ${arg.name} ready`);
consumer.subscribe(topics);
consumer.consume();
});
consumer.on("data", function(m) {
counter++;
if (counter % numMessages === 0) {
console.log("calling commit");
consumer.commit(m);
}
console.log(m.value.toString());
});
consumer.on("disconnected", function(arg) {
process.exit();
});
consumer.on('event.error', function(err) {
console.error(err);
process.exit(1);
});
consumer.on('event.log', function(log) {
console.log(log);
});
consumer.connect();
setTimeout(function() {
consumer.disconnect();
}, 300000);