PHP code example of glsv / yii2kafka

1. Go to this page and download the library: Download glsv/yii2kafka library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

glsv / yii2kafka example snippets

 composer 
 composer 
 composer 

components = [
    'class' => \yii2Kafka\YiiKafkaComponent::class,
    'brokers' => ['kafka:9092']
    /**
     * https://github.com/weiboad/kafka-php
     */
    'adapter' => [
        'class' => \yii2Kafka\adapters\nmred\NmredAdapter::class,
        'params' => [
            ////////
            // Use the params for the client at the link:
            // https://github.com/weiboad/kafka-php/blob/master/docs/Configure.md
            ////////       
            'global' => [],
            'producer' => [
                'isAsyn' => false,
                '

/**
 * @var YiiKafkaComponent $kafka
 */
$kafka = \Yii::$app->kafka;

$consumer = $kafka->getConsumer();
$consumer->consume(function ($message) {
    var_dump($message);
});

/**
 * @var YiiKafkaComponent $kafka
 */
$kafka = \Yii::$app->kafka;
$topicName = 'another-topic';

$config = new ConsumerConfig($topicName);
$config->setClientId('client_2');
$config->setGroupIp('group_2');

$consumer = $kafka->getAdapterClient()->createConsumer($config);
$consumer->consume(function ($message) {
    var_dump($message);
});