Skip to content

Realtime

A page can subscribe to broadcast events and react to them on the client — show a toast, raise a callout, or reload the page — without writing any JavaScript. You declare the listeners on the server; Lattice wires up the websocket subscription and runs the effects when an event arrives.

It is built on Laravel Echo and a broadcasting backend such as Reverb.

Override listeners() on a page and return one or more Listen declarations. Each names a channel, the broadcast event(s) to react to, and the effects to dispatch:

use Lattice\Lattice\Realtime\Listen;
protected function listeners(): array
{
return [
Listen::channel('orders')
->on('.OrderShipped')
->toast('An order just shipped'),
];
}

When an OrderShipped event broadcasts on the orders channel, every connected client viewing the page shows the toast. The leading dot (.OrderShipped) matches a broadcast name as-is; drop it to use Laravel’s namespaced event class convention.

Choose the channel type with the matching constructor:

Listen::channel('orders'); // public
Listen::private('orders.42'); // private — requires channel authorization
Listen::presence('room.42'); // presence

Private and presence channels go through Laravel’s channel authorization, so only authorized users subscribe.

Listener effects are the broadcast-safe subset of action effects — they run with no request context, so they can’t redirect or open a modal:

EffectWhat it does
->toast($message, $variant?)Shows a toast.
->callout($callout)Raises a persistent callout (needs a Callouts slot in the layout).
->reloadPage()Reloads the current page’s props — the simplest way to pull fresh data.

Chain several on one listener, and declare several listeners per page:

Listen::private('orders.'.$request->user()->id)
->on(['.OrderShipped', '.OrderDelivered'])
->toast('Your order was updated')
->reloadPage();

Lattice mounts the listeners from the page payload for you — there is nothing to render. You only need Echo configured once in your app entry point:

import { configureEcho } from "@laravel/echo-react";
configureEcho({ broadcaster: "reverb" /* …your Reverb/Pusher config */ });

If a page declares listeners but Echo isn’t configured, Lattice logs a warning and renders nothing — the page still works, it just won’t receive realtime updates.