Blog Detail


Use Apache Kafka Producers & Consumers in your Laravel App cover image

arrow_back Use Apache Kafka Producers & Consumers in your Laravel App

Laravel 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.

Producing 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;


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.

ProducerBuilder configuration 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;

    ->withConfigOption('property-name', 'property-value')
        'property-name' => 'property-value'

Sending the message to Kafka

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'])
    ->withHeaders(['header-key' => 'header-value']);


Subscribing to a topic

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([

This package also has a lot of other features.

  • 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
  • Using custom serializers/deserializers
  • Using custom committers

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

Author : Rizwan Aslam
Rizwan Aslam

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 your project

Launch project