Scopes
The scope system is php-via's killer feature. A scope determines which clients share a signal. Change one line and state that was private becomes multiplayer.
Scope types
| Scope | Shared with | Use case |
|---|---|---|
Scope::TAB |
Nobody — this tab only | Personal UI state, form input |
Scope::ROUTE |
Every user on same URL | Live feeds, shared boards, todo lists |
Scope::SESSION |
All of this user's tabs | User preferences, auth state |
Scope::GLOBAL |
Every connected client, every route | Site-wide banners, system notifications |
'room:lobby' |
Clients you add to this custom scope | Chat rooms, collaborative documents, teams |
Setting a scope
// Set the context's primary scope
$c->scope(Scope::ROUTE);
// Add a secondary scope (this context belongs to both)
$c->addScope('room:lobby');
Calling $c->scope() with Scope::ROUTE expands to the actual route path automatically.
So on /blog/post-1, it becomes route:/blog/post-1.
Broadcasting
When signals change inside an action, clients in the same scope get updated automatically. To push updates from outside an action (e.g., a timer):
// Push to all clients on this route
$app->broadcast(Scope::routeScope('/'));
// Push to a custom scope
$app->broadcast('room:lobby');
Open a second tab, then increment both sides. One is private to you; one goes to everyone.
Custom scopes
You can create any custom scope string. Clients that share the same scope string receive each other's broadcasts:
$roomId = $request->getQueryParam('room');
$roomScope = 'room:' . $roomId;
$c->addScope($roomScope);
// All clients in this room get the update
$app->broadcast($roomScope); See the chat room example for a full implementation.