API: consumer
Source:
kafka/consumer.ts
Provides an API for configuring and managing Kafka consumers, allowing scripts to start and stop listening to specific topics.
Usage
// --------
// start.ts
// --------
import { consumer } from "sdk/kafka";
consumer.topic("topic1", "{}").startListening("my-kafka-project/my-kafka-handler", 1000);
// -------------------
// my-kafka-handler.ts
// -------------------
exports.onMessage = function(message) {
console.log("Hello from My Kafka Listener! Message: " + message);
};
exports.onError = function(error) {
console.error("Error from My Kafka Listener! Error: " + error);
};
// -------
// stop.ts
// -------
import { consumer } from "sdk/kafka";
consumer.topic("topic1", "{}").stopListening();Classes
Consumer
The Consumer class acts as the main entry point for creating and configuring
Kafka topic consumers.
Methods
topic
topic (destination:string, configuration:{[key:string]:string}={}):TopicCreates a new topic configuration wrapper that can be used to start or
stop listening for messages on a Kafka topic.
@param destination The name of the Kafka topic to consume messages from.
@param configuration Optional key-value object containing Kafka consumer properties
(e.g., 'group.id', 'auto.offset.reset').
@returns A {@link Topic} instance configured for the specified destination and properties.
Topic
Represents a configured Kafka topic consumer capable of starting and stopping
background message listening.
Methods
startListening
startListening (handler:string, timeout:number):voidStarts listening to the configured topic in a background process.
@param handler The path to the script or function name that will handle the incoming Kafka messages.
This function should accept two arguments:message(string) andheaders(object).
@param timeout The maximum amount of time (in milliseconds) the consumer should wait for messages.
stopListening
stopListening ():voidStops the background process that is listening to the configured topic.
Note: Stopping is based on matching the topic and configuration, so the same
configuration object used instartListeningshould be used here.