KafkaClient class

Experimental native Kafka client with validated TLS and optional PLAIN SASL. TLS is never downgraded and SASL credentials are never sent without TLS.

Constructors

KafkaClient({required List<Broker> brokers, Duration requestTimeout = const Duration(seconds: 10), Duration connectionTimeout = const Duration(seconds: 5), int maxFrameBytes = 16 * 1024 * 1024, KafkaTlsConfig? tls, bool allowTlsBootstrapFallback = false, KafkaTlsFetchRecovery? tlsFetchRecovery, KafkaSaslPlain? sasl, int maxInFlightRequests = 100, int maxConnections = 8, int maxPendingRequestBytes = 32 * 1024 * 1024, int maxBufferedResponseBytes = 32 * 1024 * 1024, int maxReadRetries = 2, Duration retryBaseDelay = const Duration(milliseconds: 50)})
Creates a client without connecting until its first operation.

Properties

activeSubscriptionCount → int
Listened topic streams whose pumps have not finished, including paused ones. Unlistened streams do not start work and are not counted.
no setter
admittedRequestCount → int
Application admission slots held, including canceled unsettled dispatches. This is the exact count governed by maxInFlightRequests.
no setter
allowTlsBootstrapFallback → bool
Opt in to trying the next distinct configured bootstrap endpoint after a TLS handshake failure during metadata discovery, including certificate or hostname rejection. Validation stays strict for every peer; TLS is required. Local CA/context failures remain terminal. Low-level failures may be sanitized but never traverse bootstraps. Leader TLS failures stay terminal unless high-level Fetch separately opts in with tlsFetchRecovery. Each metadata traversal is bounded; this is not an overall deadline.
final
bootstrapTlsFallbackCount → int
Metadata advances to another distinct configured endpoint after TLS handshake failure, not physical connections or successful recoveries. Concurrent traversals count independently, even with shared connections.
no setter
brokers → List<Broker>
Immutable bootstrap endpoints; discovered leader endpoints share security settings.
final
bufferedResponseBytes → int
Currently retained raw response bytes.
no setter
connectionCount → int
Number of live or connecting endpoints.
no setter
connectionTimeout → Duration
TCP/TLS connection establishment deadline.
final
hashCode → int
The hash code for this object.
no setterinherited
maxBufferedResponseBytes → int
Aggregate receive-buffer budget across all connections, not decoded heap.
final
maxConnections → int
Maximum simultaneous live or connecting broker endpoints.
final
maxFrameBytes → int
Maximum request/response frame body bytes, excluding the outer length.
final
maxInFlightRequests → int
Maximum admitted application requests; canceled unsettled dispatches keep a slot.
final
maxPendingRequestBytes → int
Aggregate serialized request-byte budget before socket flush completes.
final
maxReadRetries → int
Additional attempts (0–10) only for read-safe transient failures.
final
pendingRequestCount → int
Application requests whose caller-facing waits have not completed. Canceled requests may still retain admission until dispatch settles.
no setter
queuedRequestBytes → int
Currently reserved serialized wire bytes.
no setter
requestTimeout → Duration
Per-request deadline, including admission to an authenticated connection.
final
retryBaseDelay → Duration
Full-jitter exponential retry base; each delay is capped at one second.
final
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
sasl → KafkaSaslPlain?
Optional PLAIN authentication; TLS is mandatory when configured.
final
tls → KafkaTlsConfig?
Null retains plaintext compatibility; non-null always requires valid TLS.
final
tlsFetchRecovery → KafkaTlsFetchRecovery?
Optional operation-local TLS/stale-leader recovery for high-level Fetch. Requires TLS and allowTlsBootstrapFallback. Null preserves the ordinary read-retry policy and terminal leader TLS failures. Enabled Fetch uses this policy's total-round budget, not maxReadRetries or retryBaseDelay. Produce and low-level sends do not gain recovery or replay.
final

Methods

close() → Future<void>
Cancel local waits, close live sockets, and await bounded pending connects. This permanently closes the client; create a new client to resume. Await intended operations first: close cancels pending work rather than draining delivery acknowledgements, and pending Produce delivery may be ambiguous.
commitOffsets(String groupId, String topic, {required Map<int, int> nextOffsets, String? metadata}) → Future<void>
Explicitly stores processed next offsets for one manually assigned topic.
committedOffsets(String groupId, String topic, {required Set<int> partitions, bool requireStable = false, KafkaStableOffsetRetry? stableOffsetRetry, KafkaCancellationToken? cancellationToken}) → Future<Map<int, KafkaCommittedOffset?>>
Reads broker-stored next offsets for one explicitly selected topic.
fetch(String topic, {int partition = 0, int offset = 0, int maxWaitMs = 100, int maxBytes = 1048576}) → Future<FetchResponse>
Fetch manually assigned partition at an explicit offset; no consumer groups.
findGroupCoordinator(String groupId) → Future<Broker>
Discovers a consumer group's coordinator without joining the group.
listOffsets(String topic, {int partition = 0, required KafkaOffsetPosition position}) → Future<ListOffsetsResponse>
Explicitly looks up one partition's earliest or latest position.
metadata([Set<String>? topics]) → Future<MetadataResponse>
Read-only metadata retries transport failures within configured limits. Topic and partition error fields are returned unchanged, not converted into retryable exceptions. Callers must inspect those fields themselves. Selected topics are copied before I/O and retained across read retries.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
produce(String topic, List<KafkaRecord> records, {int partition = 0, int acks = 1, int timeoutMs = 5000}) → Future<ProduceResponse?>
Produce to the discovered partition leader. No automatic retries: an uncertain acknowledgement can mean the broker stored the records already.
send(KafkaRequest request, {Broker? broker, KafkaCancellationToken? cancellationToken}) → Future
Low-level single-use request. No automatic retries or leader discovery. acks=0 means local flush only. Canceling cannot retract sent records.
subscribe(String topic, {required Map<int, int> startOffsets, Duration pollInterval = const Duration(milliseconds: 100), Duration retryDelay = const Duration(milliseconds: 500), int maxWaitMs = 100, int maxBytes = 1048576}) → Stream<KafkaTopicRecord>
Continually reads one topic's explicitly assigned partitions and offsets.
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited