-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproducer.js
More file actions
38 lines (31 loc) · 927 Bytes
/
producer.js
File metadata and controls
38 lines (31 loc) · 927 Bytes
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
const { kafka } = require('./client');
const readline = require('readline')
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
async function init() {
const producer = kafka.producer();
console.log('Connecting producer...')
await producer.connect();
console.log('Producer Connected Successfully')
rl.setPrompt('> ');
rl.prompt();
rl.on('line', async function(line) {
const [riderName, loc] = line.split(' ');
await producer.send({
topic: 'sample-topic',
messages: [{
partition: loc.toLowerCase() === '0' ? 0 : 1,
key: 'name',
value: JSON.stringify({
name: riderName,
location: loc
})
}]
})
}).on('close', async () => {
await producer.disconnect();
})
}
init();