Kind: Type
Source: packages/microservices/external/kafka.interface.ts
Part of: Microservices
Type alias to keep compatibility with
ConsumerEachMessagePayload is a compatibility type alias for the payload passed to a Kafka consumer's eachMessage handler. It provides typed access to the consumed topic, partition, message, heartbeat function, and partition-pausing controls while preserving compatibility with KafkaJS consumer APIs.
Definition
tsEachMessagePayload
Diagram
mermaidgraph LR A[Kafka Consumer] --> B[eachMessage Handler] B --> C[ConsumerEachMessagePayload] C --> D[topic] C --> E[partition] C --> F[message] C --> G[heartbeat] C --> H[pause]
Usage
tsimport { ConsumerEachMessagePayload } from '@nestjs/microservices';
async function handleKafkaMessage(
payload: ConsumerEachMessagePayload,
): Promise<void> {
const { topic, partition, message, heartbeat } = payload;
const value = message.value?.toString();
console.log({
topic,
partition,
offset: message.offset,
value,
});
// Call during long-running message processing to keep the consumer alive.
await heartbeat();
}
AI Coding Instructions
- Use
ConsumerEachMessagePayloadwhen typing handlers that receive KafkaJSeachMessagecallback data. - Treat
message.valueas nullable; check fornullor use optional chaining before decoding it. - Preserve access to
heartbeat()in long-running handlers to avoid consumer session timeouts. - Use the provided
topic,partition, andmessage.offsetvalues for logging, tracing, and idempotency handling. - Keep this alias in place for KafkaJS compatibility instead of replacing it with a manually recreated payload shape.
Was this page helpful?