Kind: Class
Source: packages/microservices/client/client-kafka.ts
Part of: Microservices
ClientKafka is a NestJS microservices client proxy for producing Kafka messages and consuming response messages for request-response patterns. It manages Kafka producer and consumer lifecycle, topic subscriptions, response callbacks, offset commits, and access to the underlying Kafka client.
Extends: ClientProxy
Implements: ClientKafkaProxy
Methods
| Method | Signature | Returns |
|---|---|---|
subscribeToResponseOf | subscribeToResponseOf(pattern: unknown) | void |
close | close() | Promise<void> |
connect | connect() | Promise<Producer> |
bindTopics | bindTopics() | Promise<void> |
createClient | createClient() | T |
createResponseCallback | createResponseCallback() | (payload: EachMessagePayload) => any |
getConsumerAssignments | getConsumerAssignments() | void |
emitBatch | emitBatch(pattern: any, data: { messages: TInput[] }) | Observable<TResult> |
commitOffsets | commitOffsets(topicPartitions: TopicPartitionOffsetAndMetadata[]) | Promise<void> |
unwrap | unwrap() | T |
on | on(event: EventKey, callback: EventCallback) | void |
registerConsumerEventListeners | registerConsumerEventListeners() | void |
registerProducerEventListeners | registerProducerEventListeners() | void |
dispatchBatchEvent | dispatchBatchEvent(packets: ReadPacket<{ messages: TInput[] }>) | Promise<any> |
dispatchEvent | dispatchEvent(packet: OutgoingEvent) | Promise<any> |
getReplyTopicPartition | getReplyTopicPartition(topic: string) | string |
publish | publish(partialPacket: ReadPacket, callback: (packet: WritePacket) => any) | () => void |
getResponsePatternName | getResponsePatternName(pattern: string) | string |
setConsumerAssignments | setConsumerAssignments(data: ConsumerGroupJoinEvent) | void |
initializeSerializer | initializeSerializer(options: KafkaOptions['options']) | void |
initializeDeserializer | initializeDeserializer(options: KafkaOptions['options']) | void |
Properties
| Property | Type |
|---|---|
logger | any |
client | `Kafka |
parser | `KafkaParser |
initialized | `Promise |
responsePatterns | string[] |
consumerAssignments | { [key: string]: number } |
brokers | `string[] |
clientId | string |
groupId | string |
producerOnlyMode | boolean |
_consumer | `Consumer |
_producer | `Producer |
Where it refuses work
ClientKafkastops the work withErrorwhen!this._consumer— “No consumer initialized. Please, call the "connect" method first.”.ClientKafkastops the work withErrorwhen!this._producer— “No producer initialized. Please, call the "connect" method first.”.ClientKafkastops the work withErrorwhen!this.client— “Not initialized. Please call the "connect" method first.”.ClientKafkastops the work withInvalidKafkaClientTopicExceptionwhenisUndefined(minimumPartition).ClientKafkastops the work with an early return whenthis.initialized.ClientKafkastops the work with an early return when!this._consumer.
When something fails
ClientKafkahandles failure in 1 place: it turns it into a return value in all 1.
Diagram
mermaidgraph LR A[Application Service] --> B[ClientKafka] B --> C[Kafka Producer] B --> D[Kafka Consumer] C --> E[Request / Event Topics] E --> F[Kafka Brokers] F --> G[Response Topics] G --> D D --> H[Response Callback] H --> A
AI Coding Instructions
- Call
subscribeToResponseOf(pattern)beforeconnect()when usingsend()for Kafka request-response messaging. - Use
send()for request-response flows andemit()oremitBatch()for event-driven, fire-and-forget publishing. - Always close the client during application shutdown to disconnect the Kafka producer and consumer cleanly.
- Keep Kafka client and consumer settings, especially
clientId, broker addresses, and consumergroupId, consistent with deployment configuration. - Use
unwrap()only when direct access to the underlying KafkaJS client is required; prefer theClientKafkaAPIs for normal messaging.
Relationships
- IMPORTS →
Logger - IMPORTS →
loadPackage - IMPORTS →
isNil - IMPORTS →
isUndefined
Used by
2 references from 2 files. Each is a place in this repository where the symbol is actually used — go read one rather than trusting an example.
Imported by (2)
KafkaController—integration/microservices/src/kafka/kafka.controller.ts:15KafkaConcurrentController—integration/microservices/src/kafka-concurrent/kafka-concurrent.controller.ts:24
Was this page helpful?