-
Notifications
You must be signed in to change notification settings - Fork 101
Expand file tree
/
Copy pathindex.js
More file actions
43 lines (37 loc) · 1.03 KB
/
index.js
File metadata and controls
43 lines (37 loc) · 1.03 KB
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
import { Kafka } from 'kafkajs';
import eventType from '../eventType.js';
// KafkaJS-based consumer replacing node-rdkafka
const kafka = new Kafka({
clientId: 'node-kafka-consumer',
brokers: ['localhost:9092'],
});
const consumer = kafka.consumer({ groupId: 'kafka' });
async function start() {
try {
await consumer.connect();
await consumer.subscribe({ topic: 'test', fromBeginning: true });
console.log('consumer ready..');
await consumer.run({
eachMessage: async ({ topic, partition, message }) => {
try {
const decoded = eventType.fromBuffer(message.value);
console.log(`received message: ${decoded}`);
} catch (err) {
console.error('Failed to decode message', err);
}
},
});
} catch (err) {
console.error('Consumer startup failed', err);
process.exit(1);
}
}
start();
// Graceful shutdown (optional but helpful during dev)
process.on('SIGINT', async () => {
try {
await consumer.disconnect();
} finally {
process.exit(0);
}
});