Opiniated High Level producer for ensi/laravel-phprdkafka
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"
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 |
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) {
//...
}
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);
}
}
Please see CONTRIBUTING for details.
- composer install
- composer test
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.