Kind: Class
Source: packages/microservices/deserializers/kafka-response.deserializer.ts
Part of: Microservices
KafkaResponseDeserializer converts Kafka consumer messages into Nest microservice IncomingResponse objects. It extracts the correlation ID and disposal state from Kafka headers while preserving the message value as the response payload, allowing ClientKafka to match responses to pending RPC requests.
Implements: Deserializer
Methods
| Method | Signature | Returns |
|---|---|---|
deserialize | deserialize(message: any, options: Record<string, any>) | IncomingResponse |
Where it refuses work
KafkaResponseDeserializerstops the work with an early return when!isUndefined(message.headers[KafkaHeaders.NEST_ERR]).KafkaResponseDeserializerstops the work with an early return when!isUndefined(message.headers[KafkaHeaders.NEST_IS_DISPOSED]).
Diagram
mermaidgraph LR A[Kafka consumer message] --> B[KafkaResponseDeserializer.deserialize] B --> C[Read headers] C --> D[Correlation ID] C --> E[Disposed flag] B --> F[Message value] D --> G[IncomingResponse] E --> G F --> G G --> H[ClientKafka RPC response handling]
Usage
tsimport {
KafkaHeaders,
KafkaResponseDeserializer,
} from '@nestjs/microservices';
const deserializer = new KafkaResponseDeserializer();
const incomingResponse = deserializer.deserialize({
key: 'order.created',
value: {
orderId: 'order-123',
status: 'created',
},
headers: {
[KafkaHeaders.CORRELATION_ID]: 'request-abc-123',
[KafkaHeaders.NEST_IS_DISPOSED]: false,
},
});
console.log(incomingResponse);
// {
// id: 'request-abc-123',
// response: { orderId: 'order-123', status: 'created' },
// isDisposed: false
// }
AI Coding Instructions
- Preserve the Kafka correlation ID header; it is required for matching a response with the originating RPC request.
- Keep the Kafka message
valueas the response payload rather than transforming its shape in this deserializer. - Use
KafkaHeaders.CORRELATION_IDandKafkaHeaders.NEST_IS_DISPOSEDinstead of hard-coded header names. - Ensure response-producing Kafka consumers include the expected Nest Kafka headers when implementing custom request-response flows.
Relationships
- IMPORTS →
isUndefined
Was this page helpful?