ensi/laravel-phprdkafka-producer

Opiniated High Level producer for laravel-phprdkafka

0.4.0 2024-06-26 12:55 UTC

This package is auto-updated.

Last update: 2024-10-27 09:11:51 UTC


README

Latest Version on Packagist Tests Total Downloads

Opiniated High Level producer for ensi/laravel-phprdkafka

Installation

Firstly, you have to install and configure ensi/laravel-phprdkafka

Then,

composer require ensi/laravel-phprdkafka-producer

Publish the config file if you need it:

php artisan vendor:publish --provider="Ensi\LaravelPhpRdKafkaProducer\LaravelPhpRdKafkaProducerServiceProvider" --tag="kafka-producer-config"

Version Compatibility

Basic Usage

Send a single message:

use Ensi\LaravelPhpRdKafkaProducer\HighLevelProducer;

(new HighLevelProducer($topicKey))->sendOne($messageString);

Send several messages at once:

(new HighLevelProducer($topicKey))->sendMany([$message1String, $message2String]);

All options with defaults:

use Ensi\LaravelPhpRdKafkaProducer\HighLevelProducer;
use Ensi\LaravelPhpRdKafkaProducer\Exceptions\KafkaProducerException;

$producer = new HighLevelProducer(
    topicKey: $topicKey, 
    producerName: 'some-producer-from-ensi/laravel-phprdkafka-config', 
    flushTimeout: 5000, // ms
    flushRetries: 5,
);

try {
    $producer
        ->setFlushTimeout(10000)
        ->setFlushRetries(10)
        ->sendOne($messageString);
} catch (KafkaProducerException $e) {
    //...
}

Middleware

You can add middleware globally via config or locally for specific Producer:

$producer->pushMiddleware(SomeMiddleware::class)
        ->sendOne($messageString);

Middleware example:

use Closure;
use Ensi\LaravelPhpRdKafkaProducer\ProducerMessage;

class SomeMiddleware
{
    public function handle(ProducerMessage $message, Closure $next): mixed
    {
        $message->headers = $message->headers ?: [];
        $message->headers['Header-Name'] = 'Header Value';

        return $next($message);
    }
}

Contributing

Please see CONTRIBUTING for details.

Testing

  1. composer install
  2. composer test

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

License

The MIT License (MIT). Please see License File for more information.