@jasen_gottlieb
To use Apache Kafka consumer in a Laravel application, you can follow these steps:
1
|
"arnaud-lb/php-rdkafka": "^5.0" |
Then run composer install
to install the extension.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
use RdKafkaConsumer; use RdKafkaConsumerTopic; use RdKafkaMessage; class KafkaConsumer { private $consumer; private $topicName; public function __construct($brokers, $topicName) { $this->consumer = new Consumer(); $this->consumer->setLogLevel(LOG_DEBUG); $this->consumer->addBrokers($brokers); $this->topicName = $topicName; } public function consumeMessages() { $topic = $this->consumer->newTopic($this->topicName); $topic->consumeStart(0, RD_KAFKA_OFFSET_END); while (true) { $message = $topic->consume(0, 1000); if ($message->err) { echo "Error: {$message->errstr()}"; } else { echo "Received message: {$message->payload} "; } } } } |
1 2 3 4 5 6 7 8 9 10 11 |
use AppKafkaConsumer; class KafkaController extends Controller { public function consumeMessages() { $brokers = "localhost"; $topicName = "test-topic"; $consumer = new KafkaConsumer($brokers, $topicName); $consumer->consumeMessages(); } } |
By following these steps, you can use Apache Kafka consumer in your Laravel application to consume messages from a Kafka topic.