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

Queued Publishing ​

Introduction ​

queue() sends an activity to a Laravel queue for a worker to publish. publish() writes it in the current process.

Configure a Laravel queue worker to process queued activities. The sync connection runs them immediately.

Queueing Activities ​

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

namespace App\Http\Controllers;

use App\Models\Order;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Storyfeed\Facades\Storyfeed;

class PlaceOrderController extends Controller
{
    public function __invoke(Request $request, Order $order): RedirectResponse
    {
        $order->update(['status' => 'placed']);

        Storyfeed::activity()
            ->by($request->user())
            ->action('place', $order)
            ->to($order->shop)
            ->queue();

        return to_route('orders.show', $order);
    }
}

After the worker publishes it:

Erica Sinclair

As with Laravel mailables, call queue() to queue the activity explicitly. It uses your configured queue connection and returns no activity.

Storyfeed::record() always publishes synchronously.

Queue and Connection ​

app/Http/Controllers/PlaceOrderController.php__invoke()
php
use Storyfeed\Facades\Storyfeed;

Storyfeed::activity()
    ->by($request->user())
    ->action('place', $order)
    ->to($order->shop)
    ->onConnection('database')
    ->onQueue('feed')
    ->queue();

After a worker handles the job:

Erica Sinclair

Laravel's Queueable methods chain before queue(), just as on a queued job. Run a worker for the chosen connection and queue:

bash
php artisan queue:work database --queue=feed

The database connection needs Laravel's jobs table.

Delays ​

Call delay() before queue() to set when the job becomes available. The builder uses delay() in place of a mailable's later():

app/Http/Controllers/PlaceOrderController.php__invoke()
php
use Storyfeed\Facades\Storyfeed;

Storyfeed::activity()
    ->by($request->user())
    ->action('place', $order)
    ->to($order->shop)
    ->delay(now()->addSeconds(10))
    ->queue();

The worker determines when the job runs. After publication:

Erica Sinclair

Per-Verb Defaults ​

routes/feed.php
php
use App\Models\Order;
use Storyfeed\Facades\Story;

Story::for(Order::class)->verb('place')
    ->headline(':actor placed :object with :target')
    ->icon('shopping-bag')
    ->onConnection('database')
    ->onQueue('feed')
    ->delay(10)
    ->afterCommit();
Erica Sinclair

Replace the verb's existing declaration with this one. Queue settings on the builder override these defaults. The declaration's delay() accepts seconds, a positive interval string, or a DateInterval.

These settings do not queue a publish() call. Call queue() to send the activity to the queue.

Queueing Story Classes ​

A Story class that implements ShouldQueue queues when published. It may also implement ShouldBeUnique. See Queueing Stories for an example. The transaction and missing-model settings below also apply.

Queues and Transactions ​

SettingQueued Publish
no explicit settingfollows the queue connection's after_commit setting
afterCommit()dispatches after the transaction commits; rollback discards it
beforeCommit()dispatches without waiting for the commit

With synchronous publish(), afterCommit() leaves the returned activity unsaved until commit. A rollback discards it. Outside a transaction, it publishes immediately.

Capturing Labels at Dispatch ​

app/Http/Controllers/PlaceOrderController.php__invoke()
php
use Storyfeed\Facades\Storyfeed;

Storyfeed::activity()
    ->by($request->user())
    ->action('place', $order)
    ->to($order->shop)
    ->snapshotNow()
    ->queue();
Erica Sinclair

By default, the worker captures model labels and details when it publishes the activity. Call snapshotNow() to capture them when queue() is called. queue() always captures published_at at dispatch unless you set publishedAt().

Missing Models ​

routes/feed.php
php
use App\Models\Order;
use App\Stories\OrderWasPlaced;
use Storyfeed\Facades\Story;

Story::for(Order::class)->verb('place', OrderWasPlaced::class)
    ->deleteWhenMissingModels();

If a model is deleted before the worker restores it, the job fails with ModelNotFoundException. This declaration discards the job without recording an activity. Replace the class binding from Story Classes with this one.

You may also call deleteWhenMissingModels() on the builder. A Story class can set public bool $deleteWhenMissingModels = true, which overrides the declaration. Models must still be restored when you use snapshotNow().

Duplicates on a Unique Index ​

routes/feed.php
php
use App\Models\Order;
use Storyfeed\Facades\Story;

Story::for(Order::class)->verb('place')->idempotent();

When your app adds its own unique index to the activities table, a retried job can hit it and fail into failed_jobs. idempotent() treats the UniqueConstraintViolationException as done: the job finishes, and publish() returns the unsaved activity. The insert rolls back to its own savepoint, so a surrounding transaction stays usable. Call ->idempotent() on one builder, or pass record(..., idempotent: true); idempotent(false) overrides the declaration. Prefer it to an exists() check before publishing, which two workers can both pass.

Carrying Actors and Context ​

By default, a queued activity uses the user authenticated when it was queued and any Storyfeed::actor() or Storyfeed::context() scope active then. The actor, the context and the signed-in user travel with the job, so this holds with afterCommit() too: the job dispatches after the commit, when the scope has already closed, and the worker still publishes as that actor. See Carrying Roles Into Queued Jobs for inherited scopes and Role Precedence for the full order.

Only Storyfeed's own scopes travel this way. Another package that adds to the job payload when it is dispatched, such as a tenant-aware queue that reads the current tenant, runs at the commit under afterCommit(). By then a block that set the tenant inside the transaction has closed. Keep that block open until after the commit, or dispatch from inside it once the transaction has committed.

Publishing From Application Jobs ​

Calling publish() inside a job records the worker's current time unless you set publishedAt(). Capture the event time in the job's constructor if the activity belongs to that earlier moment:

app/Jobs/RecordOrder.php
php
<?php

namespace App\Jobs;

use App\Models\Order;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Support\Carbon;
use Storyfeed\Facades\Storyfeed;

class RecordOrder implements ShouldQueue
{
    use Queueable;

    public Carbon $occurredAt;

    public function __construct(public Order $order)
    {
        $this->occurredAt = now();
    }

    public function handle(): void
    {
        Storyfeed::activity('place', $this->order)
            ->to($this->order->shop)
            ->publishedAt($this->occurredAt)
            ->publish();
    }
}
php
<?php

namespace App\Jobs;

use App\Models\Order;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Support\Carbon;
use Storyfeed\Facades\Storyfeed;

class RecordOrder implements ShouldQueue
{
    use Queueable;

    public Carbon $occurredAt;

    public function __construct(public Order $order)
    {
        $this->occurredAt = now();
    }

    public function handle(): void
    {
        Storyfeed::record(
            verb: 'place',
            object: $this->order,
            target: $this->order->shop,
            publishedAt: $this->occurredAt,
        );
    }
}
Erica Sinclair

Publication time determines ordering and which burst the activity joins. Also capture any event values the job needs, since restored models contain the worker's current data. Calling queue() directly already captures publication time at dispatch.

Testing Queued Publishing ​

Use Storyfeed::fake() and assertQueued() to check the queued activity without sending a job. Testing Queued and Event Publishing covers the queued assertions and when to use Laravel's queue fake instead.

Released under the MIT License.