Skip to content

ensi-platform/laravel-php-rdkafka-producer

Repository files navigation

Laravel PHP Rdkafka Producer

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

Laravel rdkakfa-producer Laravel PHP ensi/laravel-phprdkafka
^0.1.0 ^7.x || ^8.x ^8.0 ^0.1.4
^0.2.0 ^7.x || ^8.x ^8.0 ^0.1.4
^0.2.1 ^7.x || ^8.x ^8.0 ^0.2
^0.2.3 ^8.x || ^9.x ^8.0 ^0.2
^0.3.0 ^8.x || ^9.x ^8.0 ^0.3.0
^0.3.2 ^8.x || ^9.x || ^10.x ^8.0 ^0.3.0
^0.3.3 ^8.x || ^9.x || ^10.x || ^11.x ^8.0 ^0.3.4
^0.4.0 ^9.x || ^10.x || ^11.x ^8.1 ^0.4.0

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.