Pre-1.0. Not intended for public use yet. The API is subject to undocumented change, and these pages may already be wrong about it.

Skip to content

Publishing From Events ​

Introduction ​

Storyfeed is a side effect, never the source. Dispatch your own domain event where the transition succeeds and have it implement Storyfeed\Contracts\PublishesToFeed; core's listener publishes the activity its toFeedActivity() returns. This is the default way to publish. Publish inline with the builder only where there is no domain transition: imports, backfills, console commands and one-offs.

Your listeners can also react when Storyfeed publishes or deletes an activity, for feed presentation only.

Publishing From an Event ​

A controller dispatches an event:

app/Http/Controllers/StripeWebhookController.php
php
<?php

namespace App\Http\Controllers;

use App\Events\OrderPaid;
use App\Models\Order;
use Illuminate\Http\Request;
use Illuminate\Http\Response;

class StripeWebhookController extends Controller
{
    public function __invoke(Request $request): Response
    {
        $order = Order::where('payment_intent', $request->input('data.object.id'))
            ->firstOrFail();

        OrderPaid::dispatch($order); 

        return response()->noContent();
    }
}

To publish an activity when an event is dispatched, implement the Storyfeed\Contracts\PublishesToFeed interface on the event class. The interface requires a toFeedActivity method, which returns the activity to publish:

app/Events/OrderPaid.php
php
<?php

namespace App\Events;

use App\Models\Order;
use Illuminate\Foundation\Events\Dispatchable;
use Storyfeed\Contracts\PublishesToFeed;
use Storyfeed\Facades\Storyfeed;
use Storyfeed\PendingActivity;

class OrderPaid implements PublishesToFeed
{
    use Dispatchable;

    public function __construct(public Order $order) {}

    public function toFeedActivity(): ?PendingActivity
    {
        return Storyfeed::activity()
            ->by('Stripe')
            ->action('pay', $this->order);
    }
}

Listeners handle tasks such as marking the order paid:

app/Listeners/MarkOrderPaid.php
php
<?php

namespace App\Listeners;

use App\Events\OrderPaid;

class MarkOrderPaid
{
    public function handle(OrderPaid $event): void
    {
        $event->order->update(['paid_at' => now()]);

    }
}

When an event implementing PublishesToFeed is dispatched, Storyfeed calls its toFeedActivity method and publishes the returned activity:

S
Stripe marked Order #1035 paid

NOTE

Return the activity without calling its publish method. Storyfeed publishes what toFeedActivity returns, and publish returns the stored Activity, not the PendingActivity the method must return.

Listener Order ​

Core's listener is registered on the PublishesToFeed interface, so it runs after the event's own class listeners. With a sync queue, your business listeners run before the feed row is written. That is intended: business logic must not depend on the feed.

Publishing a Deletion ​

An event that publishes a delete verb must not use SerializesModels. A queued listener cannot restore a hard-deleted model, so pass the event the data the activity needs, such as the model's key and label, instead of the model.

Skipping Publication ​

Return null to skip publishing for this event:

app/Events/OrderPaid.phptoFeedActivity()
php
public function toFeedActivity(): ?PendingActivity
{
    if ($this->order->isTest()) {
        return null;
    }

    return Storyfeed::activity()
        ->by('Stripe')
        ->action('pay', $this->order);
}

Use the Storyfeed fake to test publishing. Do not fake the application event, because its listeners must run to call the toFeedActivity method.

Listening for Storyfeed Events ​

WARNING

Never put business logic (access rules, billing, subscriptions, mail, notification routing) in a listener on ActivityPublished or ActivityDeleted. Those events don't fire while recording is off, under Storyfeed::fake(), or for an idempotent duplicate, so the logic would silently stop. Put it on your own domain events, and use Storyfeed's events only for feed presentation, such as cache busting or a live-feed broadcast.

Storyfeed dispatches an event when an activity is published or deleted:

EventPayload
Storyfeed\Events\ActivityPublished$event->activity: the published activity's values
Storyfeed\Events\ActivityDeleted$event->activity: the deleted activity's values

$event->activity is a Storyfeed\Events\Snapshots\ActivitySnapshot: a read-only copy of the activity, not an Eloquent model. Both events are dispatched after the outermost transaction commits. A rollback dispatches neither event.

PropertyHolds
idthe activity's database ID
uidthe activity's ULID
verbthe verb
actor, object, target, context, origin, result, instrument, location, generatoreach role as an array of type, id, label, data, content, mediaType and attributedTo, or null when the role is empty
datathe activity's data
published_atthe publication time as an ISO 8601 string, or null
deleted_atthe deletion time as an ISO 8601 string, or null
forceDeletedwhether the activity was permanently deleted

Every non-null role id is a string, including integer model keys: compare with '1', not 1.

A listener for these events can implement ShouldQueue. Storyfeed::fake() does not dispatch them, so use Queue::fake() alone when asserting that a listener was queued.

Storyfeed also dispatches BatchClosed when a batch closes. See Listening for Closed Batches.

Released under the MIT License.