04
JanLaravel Kafka is a package that provides a nice way of producing and consuming Apache Kafka messages in your Laravel projects.
To install this package, you must have installed the PHP RdKafka
extension. You can follow the steps here to install rdkafka
in your system.
With RdKafka
installed, require this package with composer:
composer require mateusjunges/laravel-kafka
You can publish the package configuration using:
php artisan vendor:publish --tag=laravel-kafka-config
After installing the package, you can start producing and consuming Kafka messages.
To publish your messages to Kafka, you can use the publishOn
method, of Junges\Kafka\Facades\Kafka class
:
use Junges\Kafka\Facades\Kafka;
Kafka::publishOn('topic');
You can also specify the broker where you want to publish the message:
use Junges\Kafka\Facades\Kafka;
Kafka::publishOn('topic', 'broker');
This method returns a Junges\Kafka\Producers\ProducerBuilder::class
instance, and you can configure your message.
The ProducerBuilder
class contains a few methods to configure your Kafka producer. The following lines describe these methods.
The withConfigOption
method sets a \RdKafka\Conf::class
option. You can check all available options here. These methods set one config per call, and you can use withConfigOptions
passing an array of config
name and config value as argument. Here’s an example:
use Junges\Kafka\Facades\Kafka;
Kafka::publishOn('topic')
->withConfigOption('property-name', 'property-value')
->withConfigOptions([
'property-name' => 'property-value'
]);
After configuring all your message options, you must use the send method, to send the message to Kafka.
use Junges\Kafka\Facades\Kafka;
/** @var \Junges\Kafka\Producers\ProducerBuilder $producer */
$producer = Kafka::publishOn('topic')
->withConfigOptions(['key' => 'value'])
->withKafkaKey('your-kafka-key')
->withKafkaKey('kafka-key')
->withHeaders(['header-key' => 'header-value']);
$producer->send();
With a consumer-created, you can subscribe to a Kafka topic using the subscribe method:
use Junges\Kafka\Facades\Kafka;
$consumer = Kafka::createConsumer()->subscribe('topic');
Of course, you can subscribe to more than one topic at once, either using an array of topics or specifying one by one:
use Junges\Kafka\Facades\Kafka;
$consumer = Kafka::createConsumer()->subscribe('topic-1', 'topic-2', 'topic-n');
// Or, using array:
$consumer = Kafka::createConsumer()->subscribe([
'topic-1',
'topic-2',
'topic-n'
]);
This package also has a lot of other features.
If you wanna see all the details and features with code example, you can visit its complete documentation on Github.
Published at : 04-01-2022
I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.
Launch project