|
| 1 | +# php-pubsub-kafka |
| 2 | + |
| 3 | +A Kafka adapter for the [php-pubsub](https://github.com/Superbalist/php-pubsub) package. |
| 4 | + |
| 5 | +[](https://twitter.com/superbalist) |
| 6 | +[](https://travis-ci.org/Superbalist/php-pubsub-kafka) |
| 7 | +[](LICENSE) |
| 8 | +[](https://packagist.org/packages/superbalist/php-pubsub-kafka) |
| 9 | +[](https://packagist.org/packages/superbalist/php-pubsub-kafka) |
| 10 | + |
| 11 | + |
| 12 | +## Installation |
| 13 | + |
| 14 | +1. Install [librdkafka c library](https://github.com/edenhill/librdkafka) (Debian/Ubuntu) |
| 15 | + |
| 16 | + ```bash |
| 17 | + $ sudo apt-get install librdkafka-dev |
| 18 | + ``` |
| 19 | +2. Install the [php-rdkafka](https://github.com/arnaud-lb/php-rdkafka) PECL extension |
| 20 | + |
| 21 | + **PHP5** |
| 22 | + ```bash |
| 23 | + $ sudo pecl install channel://pecl.php.net/rdkafka-alpha |
| 24 | + ``` |
| 25 | + |
| 26 | + **PHP7** |
| 27 | + ```bash |
| 28 | + $ cd /tmp |
| 29 | + $ mkdir php-rdkafka |
| 30 | + $ cd php-rdkafka |
| 31 | + $ git clone https://github.com/arnaud-lb/php-rdkafka.git . |
| 32 | + $ git checkout php7 |
| 33 | + $ phpize |
| 34 | + $ ./configure |
| 35 | + $ make |
| 36 | + $ make install |
| 37 | + ``` |
| 38 | + |
| 39 | +3. Add the following to your php.ini file to enable the php-rdkafka extension |
| 40 | + `extension=rdkafka.so` |
| 41 | + |
| 42 | +4. `composer require superbalist/php-pubsub-kafka` |
| 43 | + |
| 44 | +## Usage |
| 45 | + |
| 46 | +```php |
| 47 | +// use this topic config for both the producer and consumer |
| 48 | +$topicConfig = new \RdKafka\TopicConf(); |
| 49 | +$topicConfig->set('auto.offset.reset', 'smallest'); |
| 50 | +$topicConfig->set('auto.commit.interval.ms', 300); |
| 51 | +
|
| 52 | +// create producer |
| 53 | +$producer = new \RdKafka\Producer(); |
| 54 | +$producer->addBrokers('127.0.0.1'); |
| 55 | +
|
| 56 | +// create consumer |
| 57 | +// see https://arnaud-lb.github.io/php-rdkafka/phpdoc/rdkafka.examples-high-level-consumer.html |
| 58 | +$config = new \RdKafka\Conf(); |
| 59 | +$config->set('group.id', 'php-pubsub'); |
| 60 | +
|
| 61 | +$consumer = new \RdKafka\Consumer($config); |
| 62 | +$consumer->addBrokers('127.0.0.1'); |
| 63 | +
|
| 64 | +$adapter = new \Superbalist\PubSub\Kafka\KafkaPubSubAdapter($producer, $consumer, $topicConfig); |
| 65 | +
|
| 66 | +// consume messages |
| 67 | +// note: this is a blocking call |
| 68 | +$adapter->subscribe('my_channel', function ($message) { |
| 69 | + var_dump($message); |
| 70 | +}); |
| 71 | +
|
| 72 | +// publish messages |
| 73 | +$adapter->publish('my_channel', 'HELLO WORLD'); |
| 74 | +$adapter->publish('my_channel', json_encode(['hello' => 'world'])); |
| 75 | +$adapter->publish('my_channel', 1); |
| 76 | +$adapter->publish('my_channel', false); |
| 77 | +``` |
| 78 | +
|
| 79 | +## Examples |
| 80 | +
|
| 81 | +The library comes with [examples](examples) for the adapter and a [Dockerfile](Dockerfile) for |
| 82 | +running the example scripts. |
| 83 | +
|
| 84 | +Run `make up`. |
| 85 | +
|
| 86 | +You will start at a `bash` prompt in the `/opt/php-pubsub` directory. |
| 87 | +
|
| 88 | +If you need another shell to publish a message to a blocking consumer, you can run `docker-compose run php-pubsub-kafka /bin/bash` |
| 89 | +
|
| 90 | +To run the examples: |
| 91 | +```bash |
| 92 | +$ php examples/KafkaConsumerExample.php |
| 93 | +$ php examples/KafkaPublishExample.php (in a separate shell) |
| 94 | +``` |
0 commit comments