The Kafka client for web artisans.

Laravel Kafka is a batteries-included Apache Kafka client for Laravel, so you can produce and consume messages at ridiculous speed.

use Junges\Kafka\Facades\Kafka;

Kafka::publish()
    ->onTopic('orders')
    ->withHeaders(['source' => 'checkout'])
    ->withBodyKey('order_id', $order->id)
    ->send();
use Junges\Kafka\Contracts\ConsumerMessage;

class ShipOrderHandler
{
    public function __invoke(ConsumerMessage $message): void
    {
        Shipment::create($message->getBody());
    }
}

Powering event-driven Laravel apps

3M downloads 724 GitHub stars PHP 8.1+ with ext-rdkafka Laravel 9+ supported MIT licensed

We're ready when your topics are

Ship event-driven apps with the Kafka-enabled framework you already love.

A fluent API for producers and consumers

Laravel Kafka has opinions on how PHP should talk to Kafka: chainable builders, invokable handlers, and sensible defaults. That is hundreds of librdkafka options you never have to think about.

  • Chainable producer API for topics, headers, keys and payloads
  • Consumer builder with subscriptions, groups and handlers
  • Invokable handler classes or plain closures
  • JSON serialization out of the box, custom serializers when you need them
Explore the package
use Junges\Kafka\Facades\Kafka;

$consumer = Kafka::consumer(['orders'])
    ->withConsumerGroupId('shipping')
    ->withHandler(new ShipOrderHandler)
    ->build();

$consumer->consume();
use Junges\Kafka\Contracts\ConsumerMessage;

class ShipOrderHandler
{
    public function __invoke(ConsumerMessage $message): void
    {
        Shipment::create($message->getBody());
    }
}
$consumer = Kafka::consumer(['payments'])
    ->withSasl(
        username: config('kafka.sasl.username'),
        password: config('kafka.sasl.password'),
        mechanisms: 'SCRAM-SHA-256',
        securityProtocol: 'SASL_SSL',
    )
    ->withMiddleware(new DiscardDuplicates)
    ->withManualCommit()
    ->withHandler(new SettlePayment)
    ->build();
use Junges\Kafka\Contracts\ConsumerMessage;

class DiscardDuplicates
{
    public function __invoke(ConsumerMessage $message, callable $next)
    {
        if (Cache::has($message->getKey())) {
            return;
        }

        return $next($message);
    }
}

Consumers built for production

Long-running processes are where Kafka clients fall apart. Laravel Kafka ships with the operational details handled, so deploys, retries and secured clusters are not your problem anymore.

  • Graceful shutdown finishes the message in flight before stopping
  • SASL and TLS authentication for protected clusters
  • Middlewares inspect and filter every message before your handler
  • Manual commits, specific offsets, partition assignment and regex subscriptions
  • Queueable handlers push work onto the Laravel queue system
Explore advanced usage

The best partner to your test suite

Swap the producer for a fake with one call and assert against everything your app publishes. Your CI never needs a running broker.

  • Kafka::fake() records every published message
  • Assert messages were published on a topic, or a number of times
  • Assert nothing was published at all
  • Mock consumers to test handlers in isolation
Explore testing
OrderShippedTest.php PASS
use Junges\Kafka\Facades\Kafka;

public function test_order_shipped_is_published(): void
{
    Kafka::fake();

    $this->post('/orders/123/ship')->assertOk();

    Kafka::assertPublishedOn('orders');
}

Create without limits.
What will you stream?

Trusted by PHP developers all over the world

3M

downloads on Packagist

724

stars on GitHub

2021

building in the open since

MIT

licensed, free forever

Sponsorship

Keep Laravel Kafka free, maintained, and improving.

Laravel Kafka is MIT licensed and built in the open. If it saves your team time, a sponsorship keeps the releases coming and the issues answered. Pick a monthly tier, or leave a one time tip.

0

downloads on Packagist

0

stars on GitHub