Skip to content

Commit a22d0c0

Browse files
committed
feat: added producer for kafka sample
Signed-off-by: Femi <[email protected]>
1 parent 113cf3c commit a22d0c0

File tree

4 files changed

+46
-6
lines changed

4 files changed

+46
-6
lines changed

examples/kafka-ex/src/admin.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable */
12
import kafka from "./client";
23

34
(async () => {

examples/kafka-ex/src/client.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable */
12
import "dotenv/config";
23
import { Kafka } from "kafkajs";
34

examples/kafka-ex/src/producer.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/* eslint-disable */
2+
import { CloudEvent, Kafka } from "cloudevents";
3+
import readline from "readline";
4+
import kafka from "./client";
5+
6+
const rl = readline.createInterface({
7+
input: process.stdin,
8+
output: process.stdout,
9+
});
10+
11+
(async () => {
12+
const producer = kafka.producer();
13+
await producer.connect();
14+
15+
rl.setPrompt("> ");
16+
rl.prompt();
17+
rl.on("line", async (line) => {
18+
const event = new CloudEvent({
19+
source: "cloudevents-producer",
20+
type: "events.cloudevents.test",
21+
datacontenttype: "text/plain",
22+
partitionkey: "1",
23+
data: line,
24+
});
25+
26+
const message = Kafka.structured(event);
27+
28+
console.log("Sending CloudEvent:", message);
29+
30+
await producer.send({
31+
topic: "events.cloudevents.test",
32+
messages: [message],
33+
});
34+
rl.prompt();
35+
});
36+
37+
rl.on("close", async () => {
38+
await producer.disconnect();
39+
});
40+
})();

examples/kafka-ex/tsconfig.json

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,11 @@
77
"strict": true,
88
"noImplicitAny": true,
99
"moduleResolution": "node",
10-
"esModuleInterop": true,
10+
"esModuleInterop": true
1111
},
1212
"include": [
1313
"src/**/*.ts",
14-
"src/**/*.js"
14+
"src/**/*.js",
1515
],
16-
"exclude": [
17-
"node_modules"
18-
]
19-
}
16+
"exclude": ["node_modules"]
17+
}

0 commit comments

Comments
 (0)