|
| 1 | +import { Subject, Observable, EMPTY } from "rxjs"; |
| 2 | +import { |
| 3 | + map, |
| 4 | + filter, |
| 5 | + retry, |
| 6 | + catchError, |
| 7 | + tap, |
| 8 | + mergeMap, |
| 9 | + bufferTime, |
| 10 | + share, |
| 11 | +} from "rxjs/operators"; |
| 12 | +import type { EachMessagePayload } from "kafkajs"; |
| 13 | +import { KafkaClient } from "./index"; |
| 14 | + |
| 15 | +export interface KafkaEvent<T = any> { |
| 16 | + topic: string; |
| 17 | + partition: number; |
| 18 | + offset: string; |
| 19 | + timestamp: string; |
| 20 | + data: T; |
| 21 | + raw: EachMessagePayload; |
| 22 | +} |
| 23 | + |
| 24 | +export class ReactiveKafkaConsumer { |
| 25 | + private eventSubject = new Subject<EachMessagePayload>(); |
| 26 | + private kafkaClient: KafkaClient; |
| 27 | + |
| 28 | + constructor(clientId: string, brokers: string[]) { |
| 29 | + this.kafkaClient = new KafkaClient(clientId, brokers); |
| 30 | + } |
| 31 | + |
| 32 | + async connect(groupId: string, topics: string[], fromBeginning = false) { |
| 33 | + await this.kafkaClient.connectConsumer(groupId, topics, fromBeginning); |
| 34 | + await this.kafkaClient.consume(async (payload) => { |
| 35 | + this.eventSubject.next(payload); |
| 36 | + }); |
| 37 | + } |
| 38 | + |
| 39 | + // Returns a shared Observable stream of parsed events |
| 40 | + getEventStream<T = any>(): Observable<KafkaEvent<T>> { |
| 41 | + return this.eventSubject.asObservable().pipe( |
| 42 | + map((payload) => this.parsePayload<T>(payload)), |
| 43 | + share() // Share stream among multiple subscribers |
| 44 | + ); |
| 45 | + } |
| 46 | + |
| 47 | + // Filter by specific topics |
| 48 | + filterByTopic<T = any>(...topics: string[]): Observable<KafkaEvent<T>> { |
| 49 | + return this.getEventStream<T>().pipe( |
| 50 | + filter((event) => topics.includes(event.topic)) |
| 51 | + ); |
| 52 | + } |
| 53 | + |
| 54 | + // Get stream with automatic retry and error handling |
| 55 | + getResilentStream<T = any>( |
| 56 | + retryCount = 3, |
| 57 | + retryDelay = 1000 |
| 58 | + ): Observable<KafkaEvent<T>> { |
| 59 | + return this.getEventStream<T>().pipe( |
| 60 | + retry({ count: retryCount, delay: retryDelay }), |
| 61 | + catchError((err) => { |
| 62 | + console.error("Stream error:", err); |
| 63 | + return EMPTY; |
| 64 | + }) |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + private parsePayload<T>(payload: EachMessagePayload): KafkaEvent<T> { |
| 69 | + return { |
| 70 | + topic: payload.topic, |
| 71 | + partition: payload.partition, |
| 72 | + offset: payload.message.offset, |
| 73 | + timestamp: payload.message.timestamp, |
| 74 | + data: JSON.parse(payload.message.value?.toString() || "{}") as T, |
| 75 | + raw: payload, |
| 76 | + }; |
| 77 | + } |
| 78 | + |
| 79 | + async disconnect() { |
| 80 | + this.eventSubject.complete(); |
| 81 | + await this.kafkaClient.disconnect(); |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +// Re-export RxJS operators for convenience |
| 86 | +export { |
| 87 | + map, |
| 88 | + filter, |
| 89 | + tap, |
| 90 | + retry, |
| 91 | + catchError, |
| 92 | + mergeMap, |
| 93 | + bufferTime, |
| 94 | + Observable, |
| 95 | + Subject, |
| 96 | + EMPTY, |
| 97 | +} from "rxjs"; |
0 commit comments