Kind: Interface
Source: packages/microservices/external/kafka.interface.ts
Part of: Microservices
TopicMessages groups a collection of Kafka Message objects under a single topic name. It is used when producing or handling batches of messages that should be sent to the same Kafka topic within the microservices integration layer.
Properties
| Property | Type |
|---|---|
topic | string |
messages | Message[] |
Diagram
mermaidgraph LR T[TopicMessages] --> Topic[topic: string] T --> Messages[messages: Message[]] Messages --> M1[Kafka Message] Messages --> M2[Kafka Message]
Usage
tsimport type { Message } from 'kafkajs';
import type { TopicMessages } from './kafka.interface';
const messages: Message[] = [
{
key: 'user-123',
value: JSON.stringify({
event: 'user.created',
userId: 'user-123',
}),
},
];
const topicMessages: TopicMessages = {
topic: 'user-events',
messages,
};
// Example: pass grouped messages to a Kafka producer operation.
await producer.send({
topic: topicMessages.topic,
messages: topicMessages.messages,
});
AI Coding Instructions
- Always provide a non-empty
topicthat matches the configured Kafka topic naming conventions. - Populate
messageswith Kafka-compatibleMessageobjects, including serializedvaluepayloads where required. - Group only messages targeting the same topic in a single
TopicMessagesobject. - Preserve message keys when ordering, partition affinity, or consumer routing depends on them.
- Validate payload serialization and topic configuration before passing this object to producer APIs.
How it works
TopicMessages is an exported TypeScript interface in a file intended to represent KafkaJS package types rather than NestJS logic. packages/microservices/external/kafka.interface.ts:1-4 packages/microservices/external/kafka.interface.ts:759-762
It describes one topic’s set of producer messages:
topicis required and has typestring. packages/microservices/external/kafka.interface.ts:759-761messagesis required and has typeMessage[]. packages/microservices/external/kafka.interface.ts:759-762- Each
Messagerequires avalueofBuffer,string, ornull; it can also contain an optional key, partition, headers, and timestamp. packages/microservices/external/kafka.interface.ts:121-127
ProducerBatch.topicMessages optionally accepts an array of TopicMessages, and sendBatch accepts that ProducerBatch and returns Promise<RecordMetadata[]>. packages/microservices/external/kafka.interface.ts:764-769 packages/microservices/external/kafka.interface.ts:785-788
This interface declares no runtime validation, thrown errors, or side effects. packages/microservices/external/kafka.interface.ts:759-762
Was this page helpful?