-
Notifications
You must be signed in to change notification settings - Fork 0
/
consume.ts
54 lines (45 loc) · 1.31 KB
/
consume.ts
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
49
50
51
52
53
54
import { EachMessagePayload, Kafka } from "npm:kafkajs@2.2.4";
const username = Deno.env.get("KAFKA_USERNAME");
const password = Deno.env.get("KAFKA_PASSWORD");
const broker = Deno.env.get("KAFKA_BROKER");
const SASL_MECHANISM = "scram-sha-256";
if (!username) {
throw new Error("Please set the 'KAFKA_USERNAME' environment variable");
}
if (!password) {
throw new Error("Please set the 'KAFKA_PASSWORD' environment variable");
}
if (!broker) {
throw new Error("Please set the 'KAFKA_BROKER' environment variable");
}
const redpanda = new Kafka({
brokers: [broker],
ssl: {},
sasl: {
mechanism: SASL_MECHANISM,
username,
password,
},
});
const consumer = redpanda.consumer({
groupId: "dice-enjoyers",
});
const run = async () => {
await consumer.connect();
await consumer.subscribe({
topic: "roll",
fromBeginning: true,
});
await consumer.run({
// deno-lint-ignore require-await
eachMessage: async (msg: EachMessagePayload) => {
const { topic, partition, message } = msg;
console.log();
const topicInfo = `topic: ${topic} (${partition}|${message.offset})`;
const messageInfo = `key: ${message.key}, value: ${message.value}`;
console.log(`Message consumed: ${topicInfo}, ${messageInfo}`);
console.log();
},
});
};
run().catch(console.error);