Search by

wwwision / subscription-engine

bwaidelich

Subscription engine for event-sourced systems

Package info

github.com/bwaidelich/subscription-engine

pkg:composer/wwwision/subscription-engine

Fund package maintenance!

bwaidelich

Paypal

Statistics

Installs: 35 630

Dependents: 6

Suggesters: 0

Stars: 4

Open Issues: 0

2.0.0 2026-11-13 15:28 UTC

This package is auto-updated.

Last update: 2026-11-20 08:54:50 UTC


README

Latest version PHP version License

Keep the projections and event handlers of your event-sourced PHP application up to date.

The engine passes new events from your event store to your subscribers and remembers how far each of them got. That makes it possible to add a subscriber at any time, rebuild a projection from the first event, and fix a failing subscriber without affecting the others.

flowchart LR
    events[(Event store)] -- new events --> engine[Subscription engine]
    engine -- handle event --> projection[Projections]
    engine -- handle event --> process[Event handlers]
    engine <-- status and position --> store[(Subscription store)]
Loading
  • Works with your stack: Adapters for different event stores and databases, or write your own
  • Rebuild at any time: Replay all events into a projection after fixing a bug or changing its schema
  • Isolated failures: A failing subscriber stops, while the others continue
  • Exactly once: A projection that uses the connection of the subscription store is saved together with its progress
  • Ready to scale: Process large backlogs in batches and run workers in parallel

Quick start

This example uses a SQLite database and needs PHP 8.3+ with pdo_sqlite. Install the engine, a subscription store, an event store and the matching adapter:

composer require wwwision/subscription-engine wwwision/subscription-engine-doctrine wwwision/subscription-engine-dcb-adapter wwwision/dcb-eventstore-doctrine

Save this as quickstart.php:

<?php

declare(strict_types=1);

use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Tools\DsnParser;
use Wwwision\DCBEventStore\Event\Event;
use Wwwision\DCBEventStore\Event\Events;
use Wwwision\DCBEventStore\SequencedEvent\SequencedEvent;
use Wwwision\DCBEventStoreDoctrine\DoctrineEventStore;
use Wwwision\SubscriptionEngine\Subscriber\Subscriber;
use Wwwision\SubscriptionEngine\Subscriber\Subscribers;
use Wwwision\SubscriptionEngine\SubscriptionEngine;
use Wwwision\SubscriptionEngineDcbAdapter\DcbEventStoreAdapter;
use Wwwision\SubscriptionEngineDoctrine\DoctrineSubscriptionStore;

require __DIR__ . '/vendor/autoload.php';

$connection = DriverManager::getConnection((new DsnParser())->parse('pdo-sqlite:///' . __DIR__ . '/demo.sqlite'));
$eventStore = DoctrineEventStore::create($connection, eventTableName: 'events');

$engine = new SubscriptionEngine(
    eventStoreAdapter: new DcbEventStoreAdapter($eventStore),
    subscriptionStore: new DoctrineSubscriptionStore($connection, tableName: 'subscriptions'),
    subscribers: Subscribers::fromArray([
        Subscriber::create(
            id: 'activity-log',
            handler: fn (SequencedEvent $event) => $connection->insert('activity_log', [
                'position' => $event->position->value,
                'type' => $event->event->type->value,
            ]),
            setup: fn () => $connection->executeStatement('CREATE TABLE IF NOT EXISTS activity_log (position INTEGER PRIMARY KEY, type TEXT NOT NULL)'),
            reset: fn () => $connection->executeStatement('DELETE FROM activity_log'),
        ),
    ]),
);

// 1. Once per deployment: Create the tables, register the subscribers and process past events
$eventStore->setup();
$engine->setup();
$engine->boot();

// 2. Store some events
$eventStore->append(Events::fromArray([
    Event::create('ProductAdded', '{"id": "p1"}'),
    Event::create('ProductRenamed', '{"id": "p1", "name": "New name"}'),
]));

// 3. Pass the new events to the subscribers
$engine->catchUpActive();

foreach ($connection->fetchAllAssociative('SELECT * FROM activity_log') as $row) {
    echo "{$row['position']}: {$row['type']}\n";
}

Then run it:

$ php quickstart.php
1: ProductAdded
2: ProductRenamed

Which packages do I need?

The engine itself contains no database code. Choose one event store adapter and one subscription store:

You need Package For
An event store adapter wwwision/subscription-engine-dcb-adapter wwwision/dcb-eventstore
wwwision/subscription-engine-neos-adapter neos/eventstore
A subscription store wwwision/subscription-engine-doctrine MySQL, MariaDB, PostgreSQL and SQLite with Doctrine DBAL
ekvedaras/subscription-engine-illuminate Laravel database
Background processing (optional) wwwision/subscription-engine-worker A long-running process with Supervisor and Kubernetes examples

Nothing suitable? See Writing your own store or adapter.

Guides

The handlers in these guides receive a SequencedEvent, as in the quick start. The event type depends on your adapter.

Add a projection

A projection builds a table, or anything else you can query, from all events. Only id and handler are required:

Subscriber::create(
    id: 'product-list',
    handler: function (SequencedEvent $event) use ($connection): void {
        // update the table for this event
    },
    setup: fn () => $connection->executeStatement('CREATE TABLE IF NOT EXISTS product_list (...)'),
    reset: fn () => $connection->executeStatement('DELETE FROM product_list'),
);
  • setup prepares what the subscriber needs. It runs with every $engine->setup() call, so it has to be repeatable
  • reset removes the data, so that the projection can be rebuilt

Important

Use the same database connection as the subscription store for everything a projection writes. Its changes are then saved together with the progress of the subscription, so that each event counts exactly once. With anything else, an event can be handled more than once.

React to new events only

Some handlers must not run for events from the past, for example one that sends an email. Set their run mode to FROM_NOW, so that they start with the next new event:

use Wwwision\SubscriptionEngine\Subscription\RunMode;

Subscriber::create(
    id: 'order-confirmation',
    handler: fn (SequencedEvent $event) => $mailer->sendOrderConfirmation($event),
    runMode: RunMode::FROM_NOW,
);

Deploy

Run this once per deployment, not in every process:

$engine->setup(); // creates the tables and registers new subscribers
$engine->boot();  // passes past events to the subscribers that are new or were reset

Existing subscribers aren't affected. A new FROM_BEGINNING subscriber, the default, receives all past events during boot(). After that, it counts as active and receives new events.

Process new events

To update your projections directly after storing an event, call catchUpActive() in the same process. When it returns, the events have been handled:

$eventStore->append($events);
$engine->catchUpActive();

To keep requests fast, leave this to a process that runs in the background instead, for example wwwision/subscription-engine-worker.

Rebuild a projection

For example after fixing a bug or changing the structure of a table. This calls the reset function of the subscriber and starts over with the first event:

use Wwwision\SubscriptionEngine\Engine\SubscriptionEngineCriteria;

$criteria = SubscriptionEngineCriteria::create(['product-list']);
$engine->reset($criteria);
$engine->boot($criteria);

Warning

reset() affects all subscribers if you don't pass any criteria, and boot() passes all past events to them, including the FROM_NOW ones: A subscriber that sends emails would send them again. To skip the past events after resetting a FROM_NOW subscriber, call $engine->setup($criteria) instead of boot().

If you run a worker, or anything else that keeps processing, stop it first when using version 1.x. As of version 2.0 it can keep running.

Handle failures

If a handler throws an exception, its subscription stops with the status ERROR, and the other subscribers continue. The engine doesn't throw in this case, so check the result:

$result = $engine->catchUpActive();

if (!$result->successful()) {
    foreach ($result->errors as $error) {
        printf("%s failed at event %d: %s\n", $error->subscriptionId->value, $error->position?->value, $error->message);
    }
}

The handler may have applied the event partially, so the engine doesn't retry it. Fix the cause and rebuild the projection.

Database errors, such as a deadlock, can behave differently and are repeated automatically. See Exceptions during a catch-up.

See what is going on

Get the current state of all subscriptions, for example for a status command:

foreach ($engine->getSubscriptions() as $subscription) {
    printf("%s: %s at position %d\n", $subscription->id->value, $subscription->status->value, $subscription->position->value);
}
product-list: ACTIVE at position 42
order-confirmation: ERROR at position 17

The engine also reports what it does through engine events. Their log level allows you to filter them:

use Wwwision\SubscriptionEngine\Engine\EngineEvent\EngineEvent;

$engine->onEngineEvent(function (EngineEvent $event): void {
    if ($event->logLevel() <= LOG_WARNING) {
        error_log((string) $event);
    }
});

Process a large backlog in batches

By default, a catch-up reads to the end of the event log in a single transaction. With limit, it stops after that number of events and saves its progress, so that the transactions stay small:

do {
    $result = $engine->catchUpActive(limit: 500);
    if (!$result->successful()) {
        // handle $result->errors
    }
} while ($result->hasPendingEvents);

boot() supports limit as well. Details

Run several workers in parallel

wwwision/subscription-engine-worker does this for you: Start the same script as often as you like. It also takes care of stop signals, retries and restarts.

To write your own loop, use catchUpNextActive() and bootNext(). Each call picks one subscription that no other worker is busy with:

while (true) {
    $result = $engine->catchUpNextActive(limit: 500);
    if (!$result->successful()) {
        // handle $result->errors: The next call claims the failed subscription again,
        // leave it out through SubscriptionEngineCriteria for a while so that the others can continue
    }
    if (!$result->hasPendingEvents) {
        usleep(100_000); // nothing to do, or everything is locked by other workers
    }
}

Warning

Leave failed subscriptions out, and don't assume any order between subscribers: One can be ahead of another. Details

Cheat sheet

Methods

All methods accept an optional SubscriptionEngineCriteria to restrict them to certain subscribers.

Method What it does
setup() Creates the subscription table, registers new subscribers and calls their setup function
boot() Passes past events to subscribers in the status BOOTING and activates them
catchUpActive() Passes new events to subscribers in the status ACTIVE
reset() Calls the reset function and moves subscribers back to the first event, in the status BOOTING
getSubscriptions() Returns the state of the subscriptions. getSubscription($id) returns a single one
bootNext(), catchUpNextActive() Like boot() and catchUpActive(), for a single free subscription

Statuses

Status Meaning What to do
NEW Discovered, but not set up Call setup()
BOOTING Set up or reset, past events are pending Call boot()
ACTIVE Receives new events Nothing
ERROR A subscriber failed Fix the cause, then call reset() and boot()
RESETTING The reset function is running, or has crashed Wait. If it has crashed, call reset($criteria, force: true)
DETACHED The subscriber doesn't exist anymore Nothing if you removed it. Otherwise call reset() and boot()

Learn more

  • How it works: Statuses, guarantees, concurrency, failures and how to write your own store or adapter
  • Upgrading to 2.0: Required changes, and known problems of 1.x
  • Decisions: The reasoning behind the design

Contribution

Contributions in the form of issues or pull requests are highly appreciated. Run the checks with composer test.

License

See LICENSE

Acknowledgements

This implementation is heavily inspired by parts of the patchlevel/event-sourcing package