-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproducer.js
48 lines (40 loc) · 1.07 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
41
42
43
44
45
46
47
48
const kafka = require('./client')
const readline = require('readline')
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
async function init() {
/**
* Producer sends messages to Kafka under the specified topic
*/
// Define the producer
console.log('Creating producer...')
const producer = kafka.producer()
console.log('Producer creater.')
// Connect the producer
console.log('Connecting producer...')
await producer.connect()
console.log('Producer connected.')
// Send messages
rl.setPrompt('> ')
rl.prompt()
rl.on('line', async (line) => {
const [riderName, location] = line.split(':')
await producer.send({
topic: 'rider-updates',
messages: [
{
key: 'location-updates',
value: JSON.stringify({ name: riderName, location }),
partition: location.toLowerCase() === 'north' ? 0 : 1
}
]
})
}).on('close', async () => {
// Disconnect the producer
await producer.disconnect()
console.log('Disconnected producer.')
})
}
init()