Use Apache Kafka With Laravel

Packages

September 21st, 2021

Use Apache Kafka With Laravel

Laravel Kafka is a package for using Apache Kafka producers and consumers in your Laravel app with ease. Using the publishOn method, you can fluently configure and publish message payloads:

use Junges\Kafka\Facades\Kafka;
 
Kafka::publishOn('broker', 'topic')
->withConfigOption('property-name', 'property-value')
->withConfigOptions([
'property-name' => 'property-value'
]);

Here's an example of how you send a message to Kafka via this package in a Laravel app:

use Junges\Kafka\Facades\Kafka;
 
/** @var \Junges\Kafka\Producers\ProducerBuilder $producer */
$producer = Kafka::publishOn('broker', 'topic')
->withConfigOptions(['key' => 'value'])
->withKafkaKey('your-kafka-key')
->withKafkaKey('kafka-key')
->withHeaders(['header-key' => 'header-value']);
 
$producer->send();

And here's an example of subscribing to messages via a consumer:

use Junges\Kafka\Facades\Kafka;
 
$consumer = Kafka::createConsumer('broker')->subscribe('topic');
 
// Handler via callback:
$consumer->withHandler(function(\RdKafka\Message $message) {
// Handle your message here
});
 
// Invokable handler:
class Handler
{
public function __invoke(\RdKafka\Message $message){
// Handle your message here
}
}
 
$consumer->withHandler(Handler::class)

Other features this package provides as found in the readme:

  • Configure max messages to be consumed
  • Configure a Dead letter queue - Wikipedia
  • Configure middleware
  • Kafka::fake() method for faking a Kafka producer in tests
  • Method to enable debugging during development
  • Configurable message payloads

Under-the-hood, this package requires the rdkafka extension, which provides a production-ready, fast Kafka client for PHP. You can learn more about this package, get full installation instructions, and view the source code on GitHub.


This package was submitted to our Laravel News Links section. Links is a place the community can post packages and tutorials around the Laravel ecosystem. Follow along on Twitter @LaravelLinks

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.