-
Notifications
You must be signed in to change notification settings - Fork 0
/
producer.js
42 lines (30 loc) · 978 Bytes
/
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
41
42
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!");
rl.setPrompt('>');
rl.prompt();
rl.on('line', async function (line) {
const [riderName, location] = line.split(" ");
await producer.send({
topic: "rider-updates",
messages: [
{
partition: location.toLowerCase() === "north" ? 0 : 1,
key: 'location-update', value: JSON.stringify({ name: riderName, loc: location}),
},
],
});
}).on('close', async () => {
await producer.disconnect();
console.log("Producer disconnected");
})
}
init();