Examples menu

Todo List multiplayer

Add, tick off and delete todos on a list that everyone on this page shares. The list is a static array, your draft is a TAB #[Signal], and #[Broadcast(Scope::ROUTE)] sends every change to all viewers.

#[Broadcast(Scope::ROUTE)] on the class sets ROUTE as the primary scope. A bare $ctx->broadcast() then re-renders the list for every browser on this route. No scope argument needed.
Mixed state: the todo list is a plain static array (shared across the worker, no attribute required), while the input is a TAB-scoped #[Signal] so your draft stays private.
#[Action] methods mutate the static array, then call $ctx->broadcast(). Adding clears the draft via $this->newTodo = '', which syncs back to the input automatically.
Data closure: the view passes its data as a closure, so self::$todos is re-read on every render. An array would freeze the data captured at setup.
One render per tab (the default), since each tab shows its own draft. block: 'demo' sends only the block with the #todo-list on updates, which keeps SSE payloads small.
One worker: this demo keeps the todo list in PHP statics, which each worker holds on its own. See What stays per worker before you run it with withWorkerNum().
  • Buy milk
  • Walk dog
  • Learn php-via

Total: 3 items · 1 completed

Signals

newTodo string tab scope initial: empty string

#[Signal] draft input, private per tab so your typing doesn't leak to others.

Actions

addTodo

Appends the trimmed draft to the static list, clears it, and broadcasts to all viewers.

deleteTodo

Removes a todo by ID (from $ctx->input('id')) and broadcasts the updated list.

toggleTodo

Flips the completed state of a todo and broadcasts.

Views

todo.html.twig

Renders only the demo block, the #todo-list, on updates.