Changes on the web, in the open windows
Freddy's colleague edits a team note in the browser. On Freddy's desktop the open list changes a moment later, without a refresh and without polling. Laravel announced the change; Elyra listened, the way Echo does in a browser.
Follow a channel
App::new().backend(
Backend::new("https://notes.example.com")
.broadcasting(Broadcasting::pusher("app-key")) // REVERB_APP_KEY
.follow("notes", "/api/notes"), // private-notes: re-run what read /api/notes
)
follow ties a Laravel channel to a resource. When an event arrives on
private-notes, the live queries it could have changed re-run,
coalesced, and are pushed only if the result changed, which is chapter 7's
machinery fed from the network. An event that names its record, as Laravel's
model broadcasting does (NoteUpdated { model: { id: 12, … } }),
re-runs the lists of notes and the views of note 12, not every note Freddy has
open; any other event re-runs everything that read /api/notes. A resource generated with --backend
already follows its channel; follow_all(resources::follows()) wires
them all at once.
The connection
- The protocol is Pusher's, which Askr (with
--pusher), Reverb and Pusher speak. It goes to the backend's own host, and.host(),.port()and.scheme()point it at a Reverb on its own port. It iswssonly, except to loopback. - It opens once the user is signed in and closes on sign-out. A dropped connection reconnects, backing off from one second up to thirty, and then re-runs everything followed, because events sent in the meantime are lost.
- Private channels are authorized by the backend, as the
signed-in user, at
/api/broadcasting/auth. The app secret stays on the server. - Laravel's
->toOthers()works: while connected,BackendsendsX-Socket-ID, so Laravel can skip the app whose own write already re-ran its queries.
What each event does
- It re-runs the live queries it could have changed: the lists, and the record it names.
- It is a domain event,
BackendEvent { channel, event, data }, so chapter 4'sApp::listencan log it or raise a notification. - It reaches the windows:
import { onBackendEvent, broadcasting } from "@elyra/runtime";
onBackendEvent("notes", "NoteUpdated", (data) => toast(`${data.model.title} was updated`));
// $broadcasting.state: "connecting" | "connected" | "offline"
Freddy shows a small dot in the corner from $broadcasting.state. One
caution from the guide: the data is the server's, untrusted like any input.
Re-running a query does not read it at all, only the fact that something changed,
and which record. Laravel's presence channels work too, for who else has a note
open; the guide has them under
Who else is here.
Who else has the note open
Freddy and Ada open the same team note, he in the desktop app and she in the
browser. With Laravel's presence channels each sees the other before either
saves, the way Echo.join does on the web:
Backend::new("https://notes.example.com")
.broadcasting(Broadcasting::pusher("app-key"))
.presence(["notes.*"]) // the channels a window may join
<Card title={note.title}>
{#snippet actions()}<Presence channel={`notes.${id}`} />{/snippet}
</Card>
<Presence> shows the others' avatars; Freddy himself is left
out. The app joins each channel once for all its windows and leaves when the last
one does, and a channel .presence(..) does not list is refused before
anything is sent. Laravel's channel rule still decides who gets in, and returns
what is shown of each member:
Broadcast::channel('notes.{id}', fn ($user, $id) =>
$user->can('view', Note::find($id))
? ['id' => $user->id, 'name' => $user->name, 'avatar' => $user->avatar_url]
: false);
The edit form can say more: <Presence channel={`notes.${id}`}
editing={dirty} />, with chapter 6's bind:dirty, shows
"Ada is editing" beside her avatar, and tells Ada the same about Freddy. It is a
whisper on the presence channel, as Echo sends one, so the browser takes part
with Echo alone. It is information, not a lock: two people can still both save,
and chapter 14's conflict check is what stops a lost update. Reverb names the
sender of a whisper with its default
REVERB_APP_ACCEPT_CLIENT_EVENTS_FROM=members; Askr's
--pusher does not pass presence on.
On the Laravel side
php artisan install:broadcasting --reverb # or Askr: askr serve --pusher
// app/Models/Note.php
class Note extends Model
{
use BroadcastsEvents;
public function broadcastOn(string $event): array
{
return [new PrivateChannel('notes')];
}
}
// routes/channels.php
Broadcast::channel('notes', fn ($user) => true);
// bootstrap/app.php: channel auth under /api, with the Sanctum token
->withBroadcasting(__DIR__.'/../routes/channels.php', [
'prefix' => 'api', 'middleware' => ['api', 'auth:sanctum'],
])
Broadcast events are queued by default, so the Laravel app needs a queue worker, or
QUEUE_CONNECTION=sync. Freddy's channel rule lets every signed-in user
listen, because the whole team shares the notes; a stricter app returns a real
check there. The guide is the broadcasting section of
A Laravel backend, and the design is
RFC 0006.
What you learned
.broadcasting(Broadcasting::pusher(key)).follow(channel, path)listens like Echo, over the protocol Askr, Reverb and Pusher speak.- The connection follows sign-in, reconnects with backoff and re-runs everything followed afterwards.
- Each event re-runs live queries, is a
BackendEventin Rust, and reaches the page throughonBackendEvent. - The event's data is untrusted; it says that something changed, and which record.
<Presence>shows who else has a record open, andeditingsays who is changing it, as information, not a lock.