Looking to hire Laravel developers? Try LaraJobs

laravel-kafka-tools maintained by diswebru

Description
tools for mateusjunges/laravel-kafka
Last update
2025/03/05 16:42 (dev-master)
License
Downloads
18

Comments
comments powered by Disqus

Laravel Kafka tools

Installation

composer require diswebru/laravel-kafka-tools

Examples

Sending a message to the tests topic

use Diswebru\LaravelKafkaTools\Kafka;

Kafka::publish('topic', ['message-key' => 'message-value']);

Retrieve unprocessed messages from the tests topic and terminate the process

use Diswebru\LaravelKafkaTools\Kafka;

Kafka::consumer('topic', function (ConsumerMessage $message) {
        $data = $message->getBody();
        
        if (!isset($data['message-key']) && $data['message-key'] != 'message-value') {
            // There will be no commit
            throw new \Exception('Error message');
        }
    });

Retrieve unprocessed messages from the tests topic and terminate the process using mateusjunges/laravel-kafka

use Diswebru\LaravelKafkaTools\Infrastructure\Factories\ManuallyCommitterFactory;
use Junges\Kafka\Contracts\ConsumerMessage;
use Junges\Kafka\Facades\Kafka;

Kafka::consumer()
    ->subscribe('topic')
    ->withOptions([
        'enable.auto.commit' => 'false',
        'auto.offset.reset' => 'earliest'
    ])
    ->stopAfterLastMessage()
    ->usingCommitterFactory(new ManuallyCommitterFactory())
    ->withHandler(function (ConsumerMessage $message) {
        $data = $message->getBody();
        
        if (!isset($data['message-key']) && $data['message-key'] != 'message-value') {
            // There will be no commit
            throw new \Exception('Error message');
        }
    })
    ->build()
    ->consume();