Skip to main content

kafka_client 0.1.0 copy "kafka_client: ^0.1.0" to clipboard
kafka_client: ^0.1.0 copied to clipboard

Experimental native Dart Kafka client with tested Kafka 4.0.2 produce/fetch support.

example/kafka_client_example.dart

import 'dart:convert';
import 'dart:io';
import 'package:kafka_client/kafka_client.dart';

/// Create `dart-example` on your local plaintext broker before running.
Future<void> main() async {
  final host = Platform.environment['KAFKA_HOST'] ?? '127.0.0.1';
  final port = int.parse(Platform.environment['KAFKA_PORT'] ?? '9092');
  final client = KafkaClient(brokers: [Broker(host, port)]);
  try {
    final response = await client.produce('dart-example', [
      KafkaRecord(
          value: utf8.encode('Hello from Dart'),
          timestamp: DateTime.now().millisecondsSinceEpoch),
    ]);
    final offset =
        response!.responses.single.partitionResponses.single.baseOffset;
    final fetched = await client.fetch('dart-example', offset: offset);
    for (final record in fetched.topics.single.partitions.single.records) {
      print(
          '${record.offset}: ${record.value == null ? "<null>" : utf8.decode(record.value!)}');
    }
  } finally {
    await client.close();
  }
}
1
likes
0
points
747
downloads

Publisher

verified publisherlivnium.com

Weekly Downloads

Experimental native Dart Kafka client with tested Kafka 4.0.2 produce/fetch support.

Repository (GitHub)
View/report issues

Topics

#kafka #messaging #streaming

License

unknown (license)

More

Packages that depend on kafka_client