0

I'm using library for kafka consumer which is php-rdkafka

I created a command and add the kafka consumer code. but when I run the command it will stop when received 1 data from producer.

Command Code:

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Services\KafkaService;
use App\Events\NotificationCount;

class GetNotificationCount extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'notification:count';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Get Notification count from Kafka server';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {       
        $conf = new RdKafka\Conf();
        $conf->set('group.id', 'myConsumerGroup');
        $conf->set('metadata.broker.list', '127.0.0.1');
        $conf->set('auto.offset.reset', 'earliest');
        $conf->set('enable.partition.eof', 'true');

        $consumer = new RdKafka\KafkaConsumer($conf);
        $consumer->subscribe(['test']);
        while (true) {
            $message = $consumer->consume(120*1000);
            switch ($message->err) {
                case RD_KAFKA_RESP_ERR_NO_ERROR:
                    var_dump($message);
                    break;
                case RD_KAFKA_RESP_ERR__PARTITION_EOF:
                    echo "No more messages; will wait for more\n";
                    break;
                case RD_KAFKA_RESP_ERR__TIMED_OUT:
                    echo "Timed out\n";
                    break;
                default:
                    throw new \Exception($message->errstr(), $message->err);
                    break;
            }
        }
    }
}

How to use kafka consumer code to continue listening to the topic from kafka producer?

or is there a way to run kafka consumer code continuesly in Laravel?

Kenneth
  • 2,813
  • 3
  • 22
  • 46
  • Have you tried not throwing an exception and actually processing the message? Also what if you have no `err` attribute of the message? – OneCricketeer Nov 27 '22 at 14:55

0 Answers0