PHPackages                             kim1ne/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. kim1ne/kafka

ActiveLibrary

kim1ne/kafka
============

1.1.0(1y ago)210MITPHPPHP &gt;=8.1

Since Jan 12Pushed 1y ago1 watchersCompare

[ Source](https://github.com/kim1ne/kim1ne-kafka)[ Packagist](https://packagist.org/packages/kim1ne/kafka)[ RSS](/packages/kim1ne-kafka/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (2)Versions (3)Used By (0)

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

[](#installation)

This package can be installed as a [Composer](https://getcomposer.org/) dependency.

```
composer require kim1ne/kafka
```

Usage
-----

[](#usage)

- [Kafka worker](#kafka-worker)
- [Launch several of workers](#launch-several-of-workers)
- [API](#api)

#### Kafka Worker

[](#kafka-worker)

This is wrap of the library [RdKafka](https://arnaud.le-blanc.net/php-rdkafka-doc/phpdoc/index.html). The library uses libraries of the [ReactPHP](https://reactphp.org/) for async. Stream doesn't lock.

```
use Kim1ne\InputMessage;
use Kim1ne\Kafka\KafkaConsumer;
use Kim1ne\Kafka\KafkaWorker;
use Kim1ne\Kafka\Message;
use RdKafka\Conf;

$conf = new Conf();
$conf->set('metadata.broker.list', 'kafka:9092');
$conf->set('group.id', 'my-group');
// $conf->set(...) other settings

$worker = new KafkaWorker($conf);

$worker->subscribe(['my-topic'])

$worker
    ->on(function (Message $message, KafkaConsumer $consumer) {
        $consumer->commitAsync($message);
    })
    ->critical(function (\Throwable $throwable) {
        InputMessage::red('Error: ' . $throwable->getMessage());
    });

InputMessage::green('Start Worker');

$worker->run();
```

### Launch several of workers

[](#launch-several-of-workers)

The functional starts [event loop](https://reactphp.org/event-loop/#usage) and locks stream.

```
use Kim1ne\InputMessage;
use Kim1ne\Kafka\KafkaConsumer;
use Kim1ne\Kafka\Message;
/**
 * @var \RdKafka\Conf $conf
 */
\Kim1ne\Kafka\ParallelWorkers::start(
    (new \Kim1ne\Kafka\KafkaWorker($conf))
        ->subscribe(['topic-1'])
        ->on(function (Message $message, KafkaConsumer $consumer) {
            InputMessage::red('Message in the first worker!')
        }),
    (new \Kim1ne\Kafka\KafkaWorker($conf))
        ->subscribe(['topic-2'])
        ->on(function (Message $message, KafkaConsumer $consumer) {
            InputMessage::red('Message in the second worker!')
        }),
    // ... $workerN
);
```

### API

[](#api)

This callback will be called on message from the kafka

```
use Kim1ne\Kafka\Message;
use Kim1ne\Kafka\KafkaConsumer;

$worker
    ->on(function(Message $message, KafkaConsumer $consumer) {
        // Message!
    });
```

This callback will be called if bad message

```
use Kim1ne\Kafka\Message;

$worker
    ->error(function (Message $message) {
        // the callback for bad message
        // $message->err !== RD_KAFKA_RESP_ERR_NO_ERROR
        // except messages with error code === RD_KAFKA_RESP_ERR__TIMED_OUT
    });
```

In this callback will be called, will be thrown out an exception

```
$worker
    ->critical(function (\Throwable $e) {
        // Error
    })
```

Stops the worker. If is parallel process, that destroys the worker and if he is last, stops the event-loop

```
$worker->stop();
```

Sets timeout for call method of the [RdKafka\\Consumer::consume($timeout\_ms)](https://arnaud.le-blanc.net/php-rdkafka-doc/phpdoc/rdkafka-kafkaconsumer.consume.html)

```
$worker->setTimeoutMs(1000); // default is 0
```

Returns object of the [RdKafka\\Consumer:::class](https://arnaud.le-blanc.net/php-rdkafka-doc/phpdoc/class.rdkafka-kafkaconsumer.html)

```
$consumer = $worker->getConsumer();
```

turns off the sleep mode. Will be too many errors, the worker will continue the work

```
$worker->noSleep();
```

Returns attempts of again processing

```
/**
 * @var \Kim1ne\Kafka\Message $message
 */
$message->getAttempts();
```

commits current message, creates duplicate the message, increments attempt on 1 and sends to the end the topic. the topic may be specified, otherwise will be selected the topic of the message

```
/**
 * @var \Kim1ne\Kafka\KafkaConsumer $consumer
 */

$consumer->retry(Message $message, ?string $overrideTopicName = null, int $timeWaiting = 10_000);
```

```
use Kim1ne\Kafka\Message;
use Kim1ne\Kafka\KafkaConsumer;

$worker
    ->on(function (Message $message, KafkaConsumer $consumer) {
        $attempts = $message->getAttempts();

        if ($attempts < 3) {
            $consumer->retry($message);
            return;
        }

        $consumer->commitAsync($message);
    });
```

###  Health Score

29

—

LowBetter than 60% of packages

Maintenance46

Moderate activity, may be stable

Popularity8

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity47

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 ~31 days

Total

2

Last Release

450d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/5994f47078df908351e43136f67ea6a8369eeca0945a8d8d500863a36c26b65b?d=identicon)[kim1ne](/maintainers/kim1ne)

---

Top Contributors

[![kim1ne](https://avatars.githubusercontent.com/u/111231185?v=4)](https://github.com/kim1ne "kim1ne (1 commits)")

### Embed Badge

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

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

###  Alternatives

[composer/composer

Composer helps you declare, manage and install dependencies of PHP projects. It ensures you have the right stack everywhere.

29.4k187.2M2.6k](/packages/composer-composer)[ccxt/ccxt

A cryptocurrency trading API with more than 100 exchanges in JavaScript / TypeScript / Python / C# / PHP / Go

41.5k328.9k1](/packages/ccxt-ccxt)[league/geotools

Geo-related tools PHP 7.3+ library

1.4k5.3M26](/packages/league-geotools)[react/http

Event-driven, streaming HTTP client and server implementation for ReactPHP

78126.4M414](/packages/react-http)[laravel-workflow/laravel-workflow

Durable workflow engine that allows users to write long running persistent distributed workflows (orchestrations) in PHP powered by Laravel queues.

1.2k446.6k6](/packages/laravel-workflow-laravel-workflow)[ezimuel/ringphp

Fork of guzzle/RingPHP (abandoned) to be used with elasticsearch-php

179109.4M10](/packages/ezimuel-ringphp)

PHPackages © 2026

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