-
Notifications
You must be signed in to change notification settings - Fork 18
/
producer.js
40 lines (34 loc) · 1.1 KB
/
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
38
39
40
const Kafka = require("node-rdkafka");
const 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_TOPIC_PREFIX;
const topic = `${prefix}.test`;
const producer = new Kafka.Producer(kafkaConf);
const maxMessages = 20;
const genMessage = i => new Buffer(`Kafka example, message number ${i}`);
producer.on("ready", function(arg) {
console.log(`producer ${arg.name} ready.`);
for (var i = 0; i < maxMessages; i++) {
producer.produce(topic, -1, genMessage(i), i);
}
setTimeout(() => producer.disconnect(), 0);
});
producer.on("disconnected", function(arg) {
process.exit();
});
producer.on('event.error', function(err) {
console.error(err);
process.exit(1);
});
producer.on('event.log', function(log) {
console.log(log);
});
producer.connect();