A Kafka adapter for the php-pubsub package

2.0.0 2017-05-18 12:48 UTC

This package is auto-updated.

Last update: 2024-07-11 23:54:00 UTC


A Kafka adapter for the php-pubsub package.

Author Build Status StyleCI Software License Packagist Version Total Downloads


  1. Install librdkafka c library

    $ cd /tmp
    $ mkdir librdkafka
    $ cd librdkafka
    $ git clone .
    $ ./configure
    $ make
    $ make install
  2. Install the php-rdkafka PECL extension

    $ pecl install rdkafka
  3. Add the following to your php.ini file to enable the php-rdkafka extension

  4. composer require superbalist/php-pubsub-kafka


// create consumer
$topicConf = new \RdKafka\TopicConf();
$topicConf->set('auto.offset.reset', 'largest');

$conf = new \RdKafka\Conf();
$conf->set('', 'php-pubsub');
$conf->set('', '');
$conf->set('', 'false');
$conf->set('', 'broker');
$conf->set('', 50);

$consumer = new \RdKafka\KafkaConsumer($conf);

// create producer
$conf = new \RdKafka\Conf();
$conf->set('', 50);
$conf->set('', 20);

$producer = new \RdKafka\Producer($conf);

$adapter = new \Superbalist\PubSub\Kafka\KafkaPubSubAdapter($producer, $consumer);

// consume messages
// note: this is a blocking call
$adapter->subscribe('my_channel', function ($message) {

// publish messages
$adapter->publish('my_channel', 'HELLO WORLD');
$adapter->publish('my_channel', ['hello' => 'world']);
$adapter->publish('my_channel', 1);
$adapter->publish('my_channel', false);

// publish multiple messages
$messages = [
    ['hello' => 'world'],
    'lorem ipsum',
$adapter->publishBatch('my_channel', $messages);


The library comes with examples for the adapter and a Dockerfile for running the example scripts.

Run make up.

You will start at a bash prompt in the /opt/php-pubsub directory.

If you need another shell to publish a message to a blocking consumer, you can run make shell

To run the examples:

$ php examples/KafkaConsumerExample.php
$ php examples/KafkaPublishExample.php (in a separate shell)