Consumer
import { Kafka } from '@cookiemonsterdev/kafka-core';
const kafka = new Kafka({ clientId: 'my-app', brokers: ['localhost:9092'] });
const consumer = kafka.consumer({ groupId: 'my-group' });
// Opt in to KIP-848 on Kafka 4.0+: kafka.consumer({ groupId: 'my-group', groupProtocol: 'consumer' })
await consumer.connect();
await consumer.subscribe({ topics: ['events'], fromBeginning: true });
await consumer.run({
eachMessage: async ({ topic, partition, message }) => {
console.log({ topic, partition, offset: message.offset, value: message.value?.toString() });
},
});
message.offset is bigint. Methods: Consumer.
Source:
consumer/index.ts.
Consumers are long-running processes. Do not call run() inside a serverless
request handler.
listTopics() and partitionsFor(topic) are the same Metadata helpers as on
the producer. See Consumer API.
Subscribe
fromBeginning: true starts at the earliest offset when the group has no
commit. Pass autoOffsetReset: 'none' to throw instead. Pass
autoOffsetReset: 'by_duration:PT1H' (KIP-1106) to start at the first offset
at or after now minus that ISO-8601 duration, resolved via ListOffsets.
See
auto.offset.reset.
Choose your next step
- Process messages and batches — Choose eachMessage, eachBatch, stream, and delivery limits.
eachMessage vs eachBatch vs stream
Continue to Process messages and batches.
maxRecords
Continue to Process messages and batches.
Hooks
Continue to Process messages and batches.
- Offsets and manual assignment — Assign partitions, seek, pause, and inspect committed offsets and lag.
Pause, resume, seek
Continue to Offsets and manual assignment.
Assign mode
Continue to Offsets and manual assignment.
Committed offsets, position, and lag
Continue to Offsets and manual assignment.
- Groups and rebalances — Choose assigners and protocols, and handle partition lifecycle callbacks.
Assigners and isolation
Continue to Groups and rebalances.
Rebalance callbacks
Continue to Groups and rebalances.
- Share consumers — Acquire and acknowledge queue-like records on Kafka 4.1+.
Share groups (KIP-932)
Continue to Share consumers.
Server-side assignor and regex subscription (KIP-848)
Continue to Server-side assignor and regex subscription.
Acknowledgement: implicit vs explicit
Continue to Acknowledgement modes.
Instrumentation events
Continue to Share-consumer instrumentation.