euskadi31/kafka-php

A PHP 5.3+ framework for Apache Kafka

v1.0.1 2016-02-23 14:18 UTC

This package is not auto-updated.

Last update: 2024-12-21 19:37:22 UTC


README

Build Status SensioLabs Insight Coveralls HHVM Packagist

php-rdkafka improved to manage php-zookeeper.

Install

Add euskadi31/kafka-php to your composer.json:

% php composer.phar require euskadi31/kafka-php:~1.0

Usage

Producer

<?php

$cm = new Euskadi31\Kafka\ClusterMetadata\Zookeeper([
    '10.0.0.13' => 2181
]);

$producer = new Euskadi31\Kafka\Producer($cm);

$topic = $producer->newTopic('test');

$topic->produce(RD_KAFKA_PARTITION_UA, 0, 'Message payload');

Consumer

<?php

$cm = new Euskadi31\Kafka\ClusterMetadata\Zookeeper([
    '10.0.0.13' => 2181
]);

$conf = new RdKafka\Conf();

$consumer = new Euskadi31\Kafka\Consumer($cm, $conf);

$topic = $consumer->newTopic('test');

$topic->consumeStart(0, RD_KAFKA_OFFSET_BEGINNING);

while (true) {
    // The first argument is the partition (again).
    // The second argument is the timeout.
    $msg = $topic->consume(0, 1000);

    if (empty($msg)) {
        continue;
    }

    if ($msg->err) {
        echo $msg->errstr() . PHP_EOL;
        break;
    } else {
        echo $msg->payload . PHP_EOL;
    }
}

License

kafka-php is licensed under the MIT license.