Skip to content

Commit c793ae9

Browse files
committed
Fix types and add typescript examples
1 parent 82fce55 commit c793ae9

File tree

8 files changed

+773
-468
lines changed

8 files changed

+773
-468
lines changed

examples/typescript/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.js

examples/typescript/kafkajs.ts

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import { KafkaJS } from '@confluentinc/kafka-javascript';
2+
3+
const bootstrapServer = '<fill>';
4+
5+
async function runProducer() {
6+
const kafka = new KafkaJS.Kafka({
7+
kafkaJS: {
8+
brokers: [bootstrapServer],
9+
},
10+
});
11+
12+
const producer = kafka.producer({
13+
kafkaJS: {
14+
allowAutoTopicCreation: true,
15+
acks: 1,
16+
compression: KafkaJS.CompressionTypes.GZIP,
17+
}
18+
});
19+
20+
await producer.connect();
21+
22+
await producer.send({
23+
topic: 'test-topic',
24+
messages: [
25+
{ value: 'Hello World!', key: 'key1' },
26+
],
27+
});
28+
29+
await producer.disconnect();
30+
}
31+
32+
async function runConsumer() {
33+
const kafka = new KafkaJS.Kafka({
34+
kafkaJS: {
35+
brokers: [bootstrapServer],
36+
},
37+
});
38+
39+
const consumer = kafka.consumer({
40+
kafkaJS: {
41+
groupId: 'test-group' + Math.random(),
42+
fromBeginning: true,
43+
partitionAssigners: [KafkaJS.PartitionAssigners.roundRobin],
44+
},
45+
});
46+
47+
await consumer.connect();
48+
await consumer.subscribe({ topic: 'test-topic' });
49+
50+
await consumer.run({
51+
eachMessage: async ({ message }) => {
52+
console.log({
53+
key: message.key ? message.key.toString() : null,
54+
value: message.value ? message.value.toString() : null,
55+
});
56+
},
57+
});
58+
59+
await new Promise((resolve) => setTimeout(resolve, 30000));
60+
await consumer.disconnect();
61+
}
62+
63+
async function runAdminClient() {
64+
const kafka = new KafkaJS.Kafka({
65+
kafkaJS: {
66+
brokers: [bootstrapServer],
67+
},
68+
});
69+
70+
const admin = kafka.admin()
71+
await admin.connect();
72+
73+
await admin.createTopics({ topics: [{ topic: 'test-topic' }] });
74+
console.log("Created topic");
75+
76+
await admin.disconnect();
77+
}
78+
79+
runAdminClient()
80+
.then(runProducer)
81+
.then(runConsumer)
82+
.catch(console.error);

examples/typescript/node-rdkafka.ts

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import * as RdKafka from '@confluentinc/kafka-javascript';
2+
3+
const bootstrapServers = '<fill>';
4+
5+
function runProducer() {
6+
const producer = new RdKafka.Producer({
7+
'bootstrap.servers': bootstrapServers,
8+
'dr_msg_cb': true,
9+
});
10+
11+
producer.connect();
12+
13+
producer.on('ready', () => {
14+
console.log("Producer is ready");
15+
producer.setPollInterval(100);
16+
producer.produce('test-topic', null, Buffer.from('Hello World!'), null, Date.now());
17+
});
18+
19+
producer.on('event.error', (err) => {
20+
console.error(err);
21+
});
22+
23+
producer.on('delivery-report', (err, report) => {
24+
console.log("Delivery report received:");
25+
console.log({err, report});
26+
producer.disconnect(err => {
27+
if (err)
28+
console.log("Error disconnecting producer ", err);
29+
console.log("Disconnected producer");
30+
});
31+
});
32+
}
33+
34+
function runConsumer() {
35+
const consumer = new RdKafka.KafkaConsumer({
36+
'group.id': 'test-group',
37+
'bootstrap.servers': bootstrapServers,
38+
}, {
39+
'auto.offset.reset': 'earliest',
40+
});
41+
42+
consumer.connect();
43+
44+
consumer.on('ready', () => {
45+
console.log("Consumer is ready");
46+
consumer.subscribe(['test-topic']);
47+
consumer.consume();
48+
});
49+
50+
consumer.on('data', (data) => {
51+
console.log("Received data");
52+
console.log(data);
53+
});
54+
55+
consumer.on('event.error', (err) => {
56+
console.error(err);
57+
});
58+
59+
setTimeout(() => consumer.disconnect(), 30000);
60+
}
61+
62+
function runAdminClient() {
63+
const admin = RdKafka.AdminClient.create({
64+
"bootstrap.servers": bootstrapServers,
65+
});
66+
67+
admin.createTopic({ topic: "test-topic", num_partitions: 1, replication_factor: 1 }, (err) => {
68+
if (err) {
69+
console.error(err);
70+
admin.disconnect();
71+
return;
72+
}
73+
console.log("Created topic");
74+
admin.disconnect();
75+
});
76+
77+
}
78+
79+
// As an example, run each with some time gap to allow the prior one to finish.
80+
runAdminClient();
81+
setTimeout(runProducer, 5000);
82+
setTimeout(runConsumer, 25000);

examples/typescript/package-lock.json

+108
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/typescript/package.json

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "typescript",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "",
10+
"license": "MIT",
11+
"dependencies": {
12+
"@confluentinc/kafka-javascript": "file:../..",
13+
"node-rdkafka": "^2.18.0",
14+
"typescript": "^5.4.4"
15+
},
16+
"devDependencies": {
17+
"@types/node": "^20.12.5"
18+
}
19+
}

0 commit comments

Comments
 (0)