PHP code example of nirmalsharma / laravel-kafka-consumer

1. Go to this page and download the library: Download nirmalsharma/laravel-kafka-consumer 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/ */

    

nirmalsharma / laravel-kafka-consumer example snippets

bash
namespace App\Console\Commands;

use App\Handlers\TestHandler;
use Illuminate\Console\Command;
use KafkaConsumer;

class TestTopicConsumer extends Command
{
    protected $signature = 'kafka-consume {--partition=} {--consumer-group=} {--topic=}';

    protected $description = 'Kafka consumer!!';

    public function handle(): void
    {
      KafkaConsumer::createConsumer(new TestHandler);
    }

    public function setKafkaConfig(){
        $partition = $this->option('partition');
        if( $partition != null){
            config([
                "kafka.partition" => $partition
            ]);
        }

        $consumer_group_id = $this->option('consumer-group');
        if( !empty($consumer_group_id)){
            config([
                "kafka.consumer_group_id" => $consumer_group_id
            ]);
        }

        $topic = $this->option('topic');
        if( !empty($topic)){
            config([
                "kafka.topic" => $topic
            ]);
        }
        
    }
}

TestHandler.php
-----------------

namespace App\Handlers;

use Illuminate\Support\Facades\Log;

class TestHandler
{
  public function __invoke( $message)
  {   
    print_r([$message]);
    Log::debug('Message received!', [
        $message
    ]);
  }
}


  php artisan kafka-consume {--partition=} {--consumer-group=} {--topic=}