forked from saschak094/openslava-streaming-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
37 lines (31 loc) · 840 Bytes
/
app.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('kafka-node')
const HighLevelProducer = kafka.HighLevelProducer
const KafkaClient = kafka.KafkaClient
// build up the Kafka client zookeeper-less
const client = new KafkaClient({
kafkaHost: 'localhost:9092'
})
// configure
const argv = require('optimist').argv
const topic = argv.topic || 'openslava'
let count = 100
let rets = 0
const producer = new HighLevelProducer(client)
producer.on('ready', function () {
setInterval(send, 500)
})
producer.on('error', function (err) {
console.log('error', err)
})
function send () {
let message = JSON.stringify({
ts: new Date().toISOString()
})
producer.send([
{topic: topic, messages: [message]}
], function (err, data) {
if (err) console.log(err)
else console.log('send %d messages', ++rets)
if (rets === count) process.exit()
})
}