Search by

wwwision / subscription-engine-neos-adapter

bwaidelich

neos/eventstore adapter for wwwision/subscription-engine

Package info

github.com/bwaidelich/subscription-engine-neos-adapter

pkg:composer/wwwision/subscription-engine-neos-adapter

Fund package maintenance!

bwaidelich

Paypal

Statistics

Installs: 3

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

2.0.0 2026-11-13 15:48 UTC

This package is auto-updated.

Last update: 2026-11-13 16:02:39 UTC


README

Latest version License

Keep your projections up to date with the events of a neos/eventstore.

This package connects the event store to the Subscription Engine. Your subscribers receive each event as an EventEnvelope.

Installation

Install the adapter together with an event store implementation and a subscription store, for example:

composer require wwwision/subscription-engine-neos-adapter neos/eventstore-doctrineadapter wwwision/subscription-engine-doctrine

Usage

<?php

declare(strict_types=1);

use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Tools\DsnParser;
use Neos\EventStore\DoctrineAdapter\DoctrineEventStore;
use Neos\EventStore\Model\Event;
use Neos\EventStore\Model\Event\EventData;
use Neos\EventStore\Model\Event\EventId;
use Neos\EventStore\Model\Event\EventType;
use Neos\EventStore\Model\Event\StreamName;
use Neos\EventStore\Model\EventEnvelope;
use Neos\EventStore\Model\EventStream\ExpectedVersion;
use Psr\Clock\ClockInterface;
use Wwwision\SubscriptionEngine\Subscriber\Subscriber;
use Wwwision\SubscriptionEngine\Subscriber\Subscribers;
use Wwwision\SubscriptionEngine\Subscription\RunMode;
use Wwwision\SubscriptionEngine\SubscriptionEngine;
use Wwwision\SubscriptionEngineDoctrine\DoctrineSubscriptionStore;
use Wwwision\SubscriptionEngineNeosAdapter\NeosEventStoreAdapter;

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

$connection = DriverManager::getConnection((new DsnParser())->parse('pdo-sqlite:///' . __DIR__ . '/demo.sqlite'));
$clock = new class implements ClockInterface {
    public function now(): DateTimeImmutable
    {
        return new DateTimeImmutable();
    }
};
$eventStore = new DoctrineEventStore($connection, eventTableName: 'events', clock: $clock);

$engine = new SubscriptionEngine(
    eventStoreAdapter: new NeosEventStoreAdapter($eventStore),
    subscriptionStore: new DoctrineSubscriptionStore($connection, tableName: 'subscriptions'),
    subscribers: Subscribers::fromArray([
        Subscriber::create(
            id: 'some-projection',
            handler: fn (EventEnvelope $envelope) => print("Projecting {$envelope->event->type->value} at position {$envelope->sequenceNumber->value}\n"),
            reset: fn () => print("Resetting projection for replay\n"),
        ),
        Subscriber::create(
            id: 'some-process',
            handler: fn (EventEnvelope $envelope) => print("Invoking process for {$envelope->event->type->value}\n"),
            runMode: RunMode::FROM_NOW,
            setup: fn () => print("Setting up process\n"),
        ),
    ]),
);

// Once per deployment
$eventStore->setup();
$engine->setup();
$engine->boot();

// Store an event and pass it to the subscribers
$eventStore->commit(
    StreamName::fromString('some-stream'),
    new Event(EventId::create(), EventType::fromString('SomethingHappened'), EventData::fromString('{"foo": "bar"}')),
    ExpectedVersion::ANY(),
);
$engine->catchUpActive();
Setting up process
Invoking process for SomethingHappened
Projecting SomethingHappened at position 1

Filtered subscribers

A subscriber can declare a NeosEventFilter to receive only the events of one stream, optionally narrowed to some event types:

use Neos\EventStore\Model\Event\EventType;
use Neos\EventStore\Model\Event\EventTypes;
use Neos\EventStore\Model\EventStream\EventStreamFilter;
use Neos\EventStore\Model\EventStream\VirtualStreamName;
use Wwwision\SubscriptionEngineNeosAdapter\NeosEventFilter;

Subscriber::create(
    id: 'orders',
    handler: $handler,
    filter: NeosEventFilter::create(
        VirtualStreamName::forCategory('order-'),
        EventStreamFilter::create(EventTypes::create(EventType::fromString('OrderPlaced'))),
    ),
);

If a run contains several subscriptions, the engine applies the filters itself: Stream names are compared exactly, categories by prefix and correlation ids by equality. Avoid % and _ in categories, and case-insensitive collations, where the database would decide differently. See Filtered subscribers for how the engine applies filters.

Next steps

The guides of the Subscription Engine explain how to rebuild projections, handle failures and more. To process events in the background, see wwwision/subscription-engine-worker.

Contribution

Contributions in the form of issues or pull requests are highly appreciated

License

See LICENSE