PHPackages                             thesis/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. [Utility &amp; Helpers](/categories/utility)
4. /
5. thesis/kafka

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

thesis/kafka
============

Feature complete, pure async PHP library for Kafka.

0.1.x-dev(1mo ago)41MITPHPPHP ^8.4CI failing

Since Jul 10Pushed 1mo agoCompare

[ Source](https://github.com/thesis-php/kafka)[ Packagist](https://packagist.org/packages/thesis/kafka)[ Fund](https://www.tinkoff.ru/cf/5MqZQas2dk7)[ RSS](/packages/thesis-kafka/feed)WikiDiscussions 0.1.x Synced 1w ago

READMEChangelogDependencies (25)Versions (1)Used By (0)

Thesis Kafka
============

[](#thesis-kafka)

A pure async PHP client for Apache Kafka.

Table of contents
-----------------

[](#table-of-contents)

- [Installation](#installation)
- [Quick start](#quick-start)
- [Producer](#producer)
    - [Produce](#produce)
    - [Sync produce](#sync-produce)
    - [Idempotent producer](#idempotent-producer)
    - [Manual flush](#manual-flush)
- [Consumer](#consumer)
    - [Manual consume](#manual-consume)
    - [Group consumer](#group-consumer)
    - [Poll loop](#poll-loop)
    - [Commit](#commit)
    - [Autocommit](#autocommit)
    - [Callback API](#callback-api)
- [Logging](#logging)
- [Examples](#examples)

Installation
------------

[](#installation)

```
composer require thesis/kafka
```

Quick start
-----------

[](#quick-start)

```
use Thesis\Kafka\Client;
use Thesis\Kafka\Config;

$client = new Client(new Config(
    seeds: ['kafka-1:9092', 'kafka-2:9092', 'kafka-3:9092'],
));
```

The examples below skip the bootstrap (autoload, `Client` setup). See the [examples/](examples) directory for full runnable files.

Producer
--------

[](#producer)

Create a producer from the client. `defaultTopic` is used when a record has no topic of its own.

```
use Thesis\Kafka\Producer;

$producer = $client->createProducer(new Producer\Config(
    defaultTopic: 'events',
));
```

### Produce

[](#produce)

`produce()` buffers the record and returns a `Future`. Await it to get the result. You do not have to await right away: send many records, await later.

```
$future = $producer->produce(new Producer\Record('hello'));

$produced = $future->await();
```

### Sync produce

[](#sync-produce)

`produceSync()` sends the records and waits for the broker acknowledgement. It does not block the event loop: other coroutines keep running while it waits. Pass one record or a list.

```
$producer->produceSync([
    new Producer\Record(value: 'a', topic: 'events'),
    new Producer\Record(value: 'b', topic: 'events'),
]);
```

### Idempotent producer

[](#idempotent-producer)

Set `idempotent: true`. The broker then drops duplicates, so a retry does not write the same record twice.

```
$producer = $client->createProducer(new Producer\Config(
    defaultTopic: 'events',
    idempotent: true,
));
```

### Manual flush

[](#manual-flush)

With `manualFlush: true` the producer does not send batches on its own. You call `flush()` when you want them sent. Useful for grouping many records into few requests.

```
$producer = $client->createProducer(new Producer\Config(
    defaultTopic: 'events',
    manualFlush: true,
));

$futures = [];
for ($i = 0; $i < 100; ++$i) {
    $futures[] = $producer->produce(new Producer\Record("record={$i}"));
}

$producer->flush();
```

See [examples/produce-manual-flush.php](examples/produce-manual-flush.php).

Consumer
--------

[](#consumer)

### Manual consume

[](#manual-consume)

Without a group, the consumer reads every partition of the topic. Iterate the batches it yields.

```
$consumer = $client->createConsumer(['events']);

foreach ($consumer->consume() as $batch) {
    // handle $batch
}
```

See [examples/consume-manual.php](examples/consume-manual.php).

### Group consumer

[](#group-consumer)

Pass a `GroupConfig` to join a consumer group. Partitions are shared across all members of the group and rebalanced when members come and go.

```
use Thesis\Kafka\Consumer;

$consumer = $client->createConsumer(['events'], new Consumer\Config(
    group: new Consumer\GroupConfig(
        groupId: 'thesis-consumer',
    ),
));

foreach ($consumer->consume() as $batch) {
    // handle $batch
    $consumer->commitRecords($batch);
}
```

See [examples/consume-group.php](examples/consume-group.php).

### Poll loop

[](#poll-loop)

`ConsumeMode::Poll` returns one batch, or nothing when the timeout fires. Use it when you want to do other work between polls.

```
use Amp\TimeoutCancellation;
use Thesis\Kafka\Consumer;

while (true) {
    foreach ($consumer->consume(Consumer\ConsumeMode::Poll, new TimeoutCancellation(1.0)) as $batch) {
        // handle $batch
    }
}
```

### Commit

[](#commit)

There are two ways to commit in a group.

`commitRecords()` commits the records you pass. For each partition it commits the highest offset.

```
$consumer->commitRecords($batch);
```

`commitUncommitted()` commits the position the consumer tracked for you, across all owned partitions. You do not pass the records back. Already committed offsets are skipped, so it is a no-op when nothing new arrived.

```
$consumer->commitUncommitted();
```

See [examples/commit-uncommitted.php](examples/commit-uncommitted.php).

### Autocommit

[](#autocommit)

Set `autocommit: true` to commit the delivered position on a timer. Tune the period with `autocommitInterval`.

```
use Thesis\Time\TimeSpan;
use Thesis\Kafka\Consumer;

$consumer = $client->createConsumer(['events'], new Consumer\Config(
    group: new Consumer\GroupConfig(
        groupId: 'thesis-consumer',
        autocommit: true,
        autocommitInterval: TimeSpan::fromSeconds(5),
    ),
));
```

### Callback API

[](#callback-api)

`Client::consume()` runs a callback for each batch in the background. It returns a context you close and join on shutdown.

```
use Thesis\Kafka\Consumer;
use Thesis\Kafka\ConsumerSession;
use function Amp\trapSignal;

$ctx = $client->consume(['events'], static function (
    array $records,
    ConsumerSession $session,
): void {
    // handle $records
    $session->commitRecords($records);
}, new Consumer\Config(group: new Consumer\GroupConfig(
    groupId: 'thesis-consumer',
)));

trapSignal([\SIGINT, \SIGTERM]);
$ctx->close();
$ctx->join();
```

See [examples/consume-group-callable.php](examples/consume-group-callable.php).

Logging
-------

[](#logging)

`Client` accepts any PSR-3 logger as its second argument.

```
use Thesis\Kafka\Client;
use Thesis\Kafka\Config;

$client = new Client(new Config(
    seeds: ['kafka-1:9092'],
), $logger);
```

See [examples/logger.php](examples/logger.php).

Examples
--------

[](#examples)

Runnable examples live in the [examples](examples) directory.

###  Health Score

36

—

LowBetter than 79% of packages

Maintenance90

Actively maintained with recent releases

Popularity6

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity35

Early-stage or recently created project

 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

Unknown

Total

1

Last Release

49d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/2552865?v=4)[Valentin Udaltsov](/maintainers/vudaltsov)[@vudaltsov](https://github.com/vudaltsov)

---

Top Contributors

[![kafkiansky](https://avatars.githubusercontent.com/u/37590388?v=4)](https://github.com/kafkiansky "kafkiansky (100 commits)")

### Embed Badge

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

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

###  Alternatives

[amphp/http-server

A non-blocking HTTP application server for PHP based on Amp.

1.3k7.8M128](/packages/amphp-http-server)[laravel/framework

The Laravel Framework.

34.9k556.2M21.6k](/packages/laravel-framework)[danog/madelineproto

Async PHP client API for the telegram MTProto protocol.

3.5k920.5k24](/packages/danog-madelineproto)[grumpydictator/firefly-iii

Firefly III: a personal finances manager.

24.3k69.5k](/packages/grumpydictator-firefly-iii)[drupal/core

Drupal is an open source content management platform powering millions of websites and applications.

19467.3M1.9k](/packages/drupal-core)[drupal/core-recommended

Locked core dependencies; require this project INSTEAD OF drupal/core.

6943.5M450](/packages/drupal-core-recommended)

PHPackages © 2026

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