@qualithm/kafka-client - v0.1.6
    Preparing search index...

    Class KafkaProducer

    Kafka producer.

    Sends messages to topic partitions via the Produce API. Handles partition assignment, record batch encoding, and routing to the correct broker.

    const producer = new KafkaProducer({
    connectionPool: kafka.connectionPool,
    })
    const results = await producer.send("my-topic", [
    { key: null, value: new TextEncoder().encode("hello") },
    ])
    await producer.close()
    Index

    Constructors

    Methods

    • Abort the current transaction.

      Discards any buffered messages and sends an EndTxn(committed=false) request to the transaction coordinator.

      Returns Promise<void>

      If not in a transaction or the abort fails.

    • Begin a new transaction.

      Requires the producer to be configured with a transactional ID. Must be called before producing messages within a transaction.

      Returns void

      If not a transactional producer or already in a transaction.

    • Close the producer.

      Flushes any pending batches before closing. After closing, no more messages can be sent.

      Returns Promise<void>

    • Commit the current transaction.

      Flushes any pending messages and sends an EndTxn(committed=true) request to the transaction coordinator.

      Returns Promise<void>

      If not in a transaction or the commit fails.

    • Flush all accumulated messages immediately.

      Forces all pending batches to be sent. Resolves when all batched messages have been acknowledged (or rejected on failure).

      Returns Promise<void>

    • Send messages to a topic.

      Partitions messages using the configured partitioner, encodes them as record batches, and sends produce requests to the appropriate brokers.

      Parameters

      • topic: string

        The topic to send messages to.

      • messages: readonly Message[]

        The messages to send.

      Returns Promise<readonly ProduceResult[]>

      Produce results per partition.

      If the producer is closed or sending fails.

    • Send consumer offsets to the transaction.

      Atomically commits consumer offsets as part of the current transaction, enabling exactly-once consume-transform-produce patterns.

      Parameters

      • offsets: readonly TopicPartitionOffset[]

        The offsets to commit.

      • groupId: string

        The consumer group ID.

      Returns Promise<void>

      If not in a transaction or the operation fails.

    • Send messages targeting a specific partition directly.

      Bypasses the partitioner. The caller is responsible for choosing the correct partition index.

      Parameters

      • topicPartition: TopicPartition

        The topic and partition to send to.

      • messages: readonly Message[]

        The messages to send.

      Returns Promise<ProduceResult>

      Produce result for the partition.