PHP Client

The framework-agnostic PHP Client is the foundation every other client builds on. Use it directly when you don't have a framework, or when you want fine-grained control over how events are written and consumed.

Installation

composer require eventsourcerer/event-sourcerer-client

Configuration

use PearTreeWeb\EventSourcerer\Client\Infrastructure\{Client, Config};
use PearTreeWebLtd\EventSourcererMessageUtilities\Model\ApplicationType;

$client = new Client(new Config(
    ApplicationType::Php,
    host:          '0.0.0.0',
    url:           'https://eventsourcerer.docker.localhost',
    port:          1984,
    applicationId: 'YOUR-APPLICATION-ID',
));

Writing events

use PearTreeWebLtd\EventSourcererMessageUtilities\Model\{StreamId, EventName, EventVersion};

$client
    ->connect()
    ->writeNewEvent(
        StreamId::fromString('basket-42'),
        EventName::fromString('ItemAddedToBasket'),
        EventVersion::fromString('1'),
        ['productId' => '...', 'amount' => 1999],
    );

Listening for events (catch-up subscription)

The catchup() method replays historical events for a worker and then keeps the connection open for live events. The worker id is stored on the server so each worker resumes exactly where it left off.

use PearTreeWebLtd\EventSourcererMessageUtilities\Model\WorkerId;

$client->catchup(
    WorkerId::fromString('my-worker'),
    function (array $event): void {
        // $event has 'name', 'version', 'streamId', 'payload', 'occurredAt' ...
        handleEvent($event);
    },
);

Async, non-blocking I/O

The client is built on ReactPHP and Revolt, so a single process can keep thousands of subscriptions open. Combine it with your framework's job runner to process events with proper retries and back-off.

For Laravel and Symfony you'll usually pick the dedicated client, which wires the framework-agnostic client into the container, queue and Messenger respectively.