PHPackages                             aliftech/laravel-kafka - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. aliftech/laravel-kafka

ActiveLibrary[HTTP &amp; Networking](/categories/http)

aliftech/laravel-kafka
======================

A kafka driver for laravel

v1.0.5(4y ago)5105MITPHPPHP ^8.0

Since Dec 10Pushed 4y agoCompare

[ Source](https://github.com/Alifuz/laravel-kafka)[ Packagist](https://packagist.org/packages/aliftech/laravel-kafka)[ RSS](/packages/aliftech-laravel-kafka/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (5)Dependencies (6)Versions (8)Used By (0)

Laravel Kafka
=============

[](#laravel-kafka)

[![Latest Version On Packagist](https://camo.githubusercontent.com/4a783511a3939173289095467d24ae8292e4ccd0592e3d927fd1f23889c6fc15/687474703a2f2f706f7365722e707567782e6f72672f616c6966746563682f6c61726176656c2d6b61666b612f76)](https://packagist.org/packages/aliftech/laravel-kafka)[![Total Downloads](https://camo.githubusercontent.com/38629efe151166ca64ac5f7c808e6ab217e9dab52622da1e6f34ce9990366c6e/687474703a2f2f706f7365722e707567782e6f72672f616c6966746563682f6c61726176656c2d6b61666b612f646f776e6c6f616473)](https://packagist.org/packages/aliftech/laravel-kafka)[![MIT Licensed](https://camo.githubusercontent.com/f251623e510f5909f16ae3f4e6e548dac11340b9fde1a99be26b015b39272c00/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c6174)](LICENSE)[![PHP Version Require](https://camo.githubusercontent.com/53cc4e28efa462e24b5cf80733bdb2f8028bb5849a9b7c2622242d39bbdda87b/687474703a2f2f706f7365722e707567782e6f72672f6d61746575736a756e6765732f6c61726176656c2d6b61666b612f726571756972652f706870)](https://packagist.org/packages/mateusjunges/laravel-kafka)

Do you want to use kafka in your laravel projects? Most of the packages I've seen, does not provide an understandable syntax.

This package provides a nice way of producing (publishing) and consuming (subscribing &amp; handling) kafka messages in your Laravel projects.

Follow these docs to install this package and start using kafka in your laravel projects.

- [Installation](#installation)
- [Configuration](#configuration)
- [Publishing Messages](#publishing-messages)
    - [Concept of Producing Messages](#concept-of-producing-messages)
    - [Topics](#topics)
    - [Messages](#messages)
    - [Publishing](#publishing)
- [Subscribing To Topics](#subscribing-to-topics)
    - [Concept of Consuming Topics](#concept-of-consuming-topics)
    - [Message Handlers](#message-handlers)
        - [Creating Handlers](#creating-handlers)
        - [Handling Logics](#handling-logics)
        - [Using Middlewares](#using-middlewares)
    - [Registering Handlers](#registering-handlers)
    - [Consuming Topics](#consuming-topics)
- [Run Consumer With Supervisor](#run-consumer-with-supervisor)
- [Concept of Naming Topics](#concept-of-naming-topics)

Installation
============

[](#installation)

To install this package, you must have installed PHP RdKafka extension. First, follow the steps [here](https://github.com/edenhill/librdkafka#installation)and install rdkafka library in your system and then install PHP RdKafka [here](https://arnaud.le-blanc.net/php-rdkafka-doc/phpdoc/rdkafka.setup.html).

You may now install Laravel Kafka into your project using the Composer package manager:

```
composer require aliftech/laravel-kafka
```

After installing Laravel Kafka, publish its assets using the `kafka:install` Artisan command:

```
php artisan kafka:install

```

Configuration
=============

[](#configuration)

After publishing Kafka's assets, its primary configuration file will be located at `config/kafka.php`. (More here later)

Publishing Messages
===================

[](#publishing-messages)

A message can be published to a topic. To achieve that, you need to create a topic model and a message [DTO](https://en.wikipedia.org/wiki/Data_transfer_object).

Concept of Producing messages
-----------------------------

[](#concept-of-producing-messages)

The concept of pub/sub in kafka is accomplished with Producers and Consumers. Producers will publish messages to topics and Consumers will subscribe to topics and listen for new messages. Topics are like events and messages are like DTO objects to deliver data from one source to other.

Messages should have the same data structures when they are sent to the same topic. To solve this problem, your message should be created as a separate class with a data structure and it can only be published to a unique topic. Meaning that, single Topic should NOT receive two messages with different data structures.

Think of Topics as tables (in database). You cannot insert two records with different column structures into the table. And think of the records as your messages. You can send 2 messages with different data structure to one topic. But, this will mostly create problems instead of solving them!

Topics
------

[](#topics)

Topics are created in `./app/Kafka/Topics` folder with the following artisan command:

```
php artisan make:topic MyTopic
```

After the generation of the topic, you have to set the corresponding kafka topic key and here it's `my_topic`:

```
/**
 * Put down the correct Topic Key.
 *
 * @var string $topic_key
 */
public static string $topic_key = 'my_topic';
```

Messages
--------

[](#messages)

Messages are created in `./app/Kafka/Messages` folder with the following artisan command:

```
php artisan make:message MyMessage
```

And now, you have to connect to a corresponding topic:

```
/**
 * Topic that the message will be sent to.
 */
protected string $topic_class = MyTopic::class;
```

After this, DTO properties should be set up in the class:

```
/**
 * Message variables to be transfered to Kafka.
 * they should be public only.
 */
public int $id;
public string $name;
public string $file_url;
public bool $is_checked;
```

In addition, you could also create a constuctor function to set the properties when creating the object:

```
public function __contruct(int $id, string $name, string $file_url, bool $is) {
    $this->id = $id;
    $this->name = $name;
    $this->file_url = $file_url;
    $this->is_checked = $is_checked;
}
```

Publishing
----------

[](#publishing)

In order to publish the message to the topic, `publish()` method should be called on the message object:

```
use App\Kafka\Messages\MyMessage;

// you can create a message object
$message = MyMessage::create(1, 'My name changed', 'file2.jpg', false);
// or
$message = new MyMessage(1, 'My name changed', 'file2.jpg', false);

// you can set properties like this too
$message->id = 1;
$message->name = 'My name changed';
$message->file_url = 'file2.jpg';
$message->is_checked = false;

// now that message object is created and
// filled with data, it's ready to be published
$message->publish();
```

Now, Your message (`MyMessage`) has been sent to the topic (`MyTopic`).

Subscribing To Topics
=====================

[](#subscribing-to-topics)

A topic can be consumed like an event can be listened. And Consumers run certain handlers when there is a new message published to the topic.

Concept of Consuming Topics
---------------------------

[](#concept-of-consuming-topics)

To use Consumers, you need to create Topic model and Handlers. And then, you should attach handlers to topics. You can handle new messages inside your handlers. After sutting up topics and handlers, Artisan command `kafka:consume` can be run to subscribe to topics. This command will automatically run your handlers in exact order as you provided when there is a new message.

Message Handlers
----------------

[](#message-handlers)

Message handlers are created as separate classes to handle messages.

### Creating Handlers

[](#creating-handlers)

Handlers are created in `./app/Kafka/Handlers` folder with the following artisan command:

```
php artisan make:handler MyHandler
```

### Handling Logics

[](#handling-logics)

After generating the handler (`MyHandler`), you should put your logic in `handle` function of the handler:

```
/**
 * Handle the topic message.
 *
 * @param $message
 * @return void
 */
public function handle($message): void
{
    // handle new messages here. Put your logic here
}
```

### Using Middlewares

[](#using-middlewares)

Additionally, if you want to filter messages even before the message gets to the function `handle`, you should use middewares in handlers. For this purpose, you should use a special function called `middleware`:

```
/**
 * Middleware to be passed before handling the message
 *
 * @param $message
 * @return void
 */
public function middleware($message, Closure $next): void
{
    // here put your filters
    $next($message);
}
```

Call the second arg `$next` like a function with the message inside if you want the middleware to pass and call the `handle` function. Don't call the `$next` if `$message` cannot pass your filters or something like that. When you call `$next`, be sure to pass `$message` to the function like `$next($message)`.

Cases when you might want to use middlewares are a lot. One of the Cases is that if you want to use multiple handlers to subscribe for a single topic. You might want the new messages (coming to a single topic) to be processed by multiple hanlers and each handler might filter the messages by their meta data (in the message headers).

Registering Handlers
--------------------

[](#registering-handlers)

You have created your topics and your handlers. Now, you should set which handlers to be called when a new message is published to a certain topic. And it's set inside the service provider (called `KafkaServiceProvider`) which is published when you called Artisan command `kafka:install`:

```
use App\Kafka\Topics\MyTopic;
use App\Kafka\Handlers\MyHandler;

/**
 * The topic consumer mappings for the application.
 *
 * @var array
 */
protected $subscribe = [
    MyTopic::class => [
        MyHandler::class,
        // MyHandler2::class,
        // MyHandler3::class,
        // ...
        // You could put a lot of handlers here
    ],
];
```

Consuming Topics
----------------

[](#consuming-topics)

Now, You have set the relationship between topics and handlers. To continuesly process new message, You should call the following Artisan command:

```
php artisan kafka:consume
```

This command will keep running and be listening to new messages. But when this stops, your project will also stop handling new messages. But, kafka is intelligent and it will save your consumer's offset. And when you call the Artisan command again, your consumer will start from that offset where it stops working.

\*\* Don't run this Artisan command on production without process managers, because, it might stop working. Using Supervisor is recommended!

Run Consumer With Supervisor
============================

[](#run-consumer-with-supervisor)

Here will be docs soon!

Concept of Naming Topics
========================

[](#concept-of-naming-topics)

Here will be docs soon!

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

7

Last Release

1612d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/c6578b27161a6e523d76d5a10bda9b7b7e1b9a2d18128be0d175a5cc7e5a0c40?d=identicon)[karimov](/maintainers/karimov)

---

Top Contributors

[![KarimovJavoxir](https://avatars.githubusercontent.com/u/31111513?v=4)](https://github.com/KarimovJavoxir "KarimovJavoxir (25 commits)")

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/aliftech-laravel-kafka/health.svg)

```
[![Health](https://phpackages.com/badges/aliftech-laravel-kafka/health.svg)](https://phpackages.com/packages/aliftech-laravel-kafka)
```

###  Alternatives

[mateusjunges/laravel-kafka

A kafka driver for laravel

7163.1M17](/packages/mateusjunges-laravel-kafka)[workerman/webman

High performance HTTP Service Framework.

2.6k86.6k68](/packages/workerman-webman)[akamai-open/edgegrid-client

Implements the Akamai {OPEN} EdgeGrid Authentication specified by https://developer.akamai.com/introduction/Client\_Auth.html

482.5M6](/packages/akamai-open-edgegrid-client)[onlime/laravel-http-client-global-logger

A global logger for the Laravel HTTP Client

1935.1k](/packages/onlime-laravel-http-client-global-logger)[huaweicloud/huaweicloud-sdk-php

Huawei Cloud SDK for PHP

1829.2k2](/packages/huaweicloud-huaweicloud-sdk-php)[opiy-org/asterisk-ari-php

An object-oriented client for the Asterisk REST Interface (ARI). Handles ARI calls and events for you.

121.1k](/packages/opiy-org-asterisk-ari-php)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
