PHP code example of bhamner / laravel-redis-streams
1. Go to this page and download the library: Download bhamner/laravel-redis-streams library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
bhamner / laravel-redis-streams example snippets
use Bhamner\RedisStreams\Facades\RedisStream;
$entry = RedisStream::on('orders')->add([
'type' => 'order.placed',
'order_id' => '123',
'total' => '4900',
]);
$entry->id; // Redis id, such as 1710000000000-0
use Bhamner\RedisStreams\StreamEvent;
class OrderPlaced extends StreamEvent
{
public function __construct(
public string $orderId,
public int $total,
) {}
public function stream(): string
{
return 'orders';
}
}
OrderPlaced::dispatch(orderId: '123', total: 4900);
use Bhamner\RedisStreams\Consumer;
use Bhamner\RedisStreams\Entry;
class BillingConsumer extends Consumer
{
protected string $stream = 'orders';
protected string $group = 'billing';
public function handle(Entry $entry): void
{
$event = $entry->event(); // OrderPlaced, when the entry was dispatched that way
}
public function failed(Entry $entry, \Throwable $exception): void
{
// deliveries exhausted
}
}