-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f96c999
commit 934cff7
Showing
6 changed files
with
106 additions
and
1 deletion.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import {Controller, Get, Injectable, Logger, Post} from "@nestjs/common" | ||
import {randomUUID} from "crypto" | ||
import {Kafka, Partitioners} from "kafkajs" | ||
|
||
@Controller() | ||
export class KafkaController { | ||
constructor() { | ||
console.log("KafkaController constructor") | ||
} | ||
|
||
@Post("kafka") | ||
async postKafka() { | ||
const kafka = new Kafka({ | ||
clientId: "my-app", | ||
brokers: ["localhost:9092"], | ||
retry: { | ||
retries: 1 | ||
} | ||
}) | ||
|
||
const producer = kafka.producer({ | ||
createPartitioner: Partitioners.DefaultPartitioner | ||
}) | ||
|
||
await producer.connect() | ||
await producer.send({ | ||
topic: "test-3", | ||
messages: [{value: `Hello KafkaJS user! ${randomUUID()}`}] | ||
}) | ||
|
||
return "" | ||
} | ||
|
||
@Get("kafka") | ||
async getKafka() { | ||
const kafka = new Kafka({ | ||
clientId: "my-app", | ||
brokers: ["localhost:9092"], | ||
retry: { | ||
retries: 1 | ||
} | ||
}) | ||
|
||
const consumer = kafka.consumer({ | ||
groupId: randomUUID() | ||
}) | ||
|
||
await consumer.connect() | ||
await consumer.subscribe({topic: "test-3", fromBeginning: true}) | ||
|
||
await consumer. | ||
|
||
await consumer.run({ | ||
eachMessage: async ({topic, partition, message}) => { | ||
Logger.log({ | ||
topic, | ||
partition, | ||
offset: message.offset, | ||
value: message.value!!.toString() | ||
}) | ||
} | ||
}) | ||
|
||
return "" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters