14: WebSocket

Realtime in Phlo draait via Phlo Realtime, de WebSocket-server die is ingebouwd in de Phlo Daemon. Eén Node-proces beheert de socketverbindingen over elke vhost en voert elk evenement op je PHP-app uit via de eigen worker pool van de daemon. Je app implementeert vier hook-functies en zendt uit vanuit PHP met wsCast().

14.1: Streaming eerst

Niet alles dat realtime is, heeft een socket nodig. Voor eenrichtingsupdates via een enkele aanvraag opent chunk() (de chunk resource) een streamingresponse en verzendt elke oproep onmiddellijk als één JSON-regel via gewone HTTP; phlo.js past de commando's toe zodra ze binnenkomen:

route async POST report::generate {
	foreach ($this->steps AS $i => $step){
		$step->run
		chunk(inner: arr('#progress' => $i + 1 .'/'. count($this->steps)))
	}
	apply(toast: 'Done')
}

%res->streaming = true geeft een route hetzelfde gedrag met gewone apply() aanroepen, en klassieke SSE (text/event-stream) vult dezelfde eenrichtingsslot. Al deze houden een PHP worker vast zolang de respons streamt: precies goed voor eindig werk dat één client bekijkt (AI token streams, imports, rapporten), en precies verkeerd voor een permanente verbinding die wacht op gebeurtenissen. Dat is waar Phlo Realtime voor is: de daemon houdt de open sockets vast, jouw PHP draait alleen wanneer er een gebeurtenis aankomt, en een uitzending bereikt elke verbonden client in plaats van alleen degene die vroeg. Streaming volgt de aanvraag; broadcasting volgt de vloot van verbindingen.

14.2: Wat Phlo Realtime is

Phlo Realtime is de WebSocket-laag van de daemon (gebouwd op de ws bibliotheek), niet een apart proces. De enkele daemon op poort 3001 bedient de hele stack: hij accepteert de socket-upgrades, routeert op basis van de Host header van de handshake, houdt het clientregister bij, draait de /message broadcast bridge en dispatcht elk socket-evenement naar jouw PHP op dezelfde worker pool die de daemon al voor alles gebruikt.

Omdat die dispatch in-process is, is er niets te verbinden tussen "de socketlaag" en "de PHP-laag": ze zijn één proces. Elk evenement (auth, connect, receive, close) draait de bijpassende websocket::<hook> target, en de uitvoeringsmodus is wat de daemon al doet voor die host:

Hoe dan ook, elk evenement geeft de handler de volledige levenscyclus van het verzoek: DB, sessie, resources, alles is eenvoudig beschikbaar. De modus volgt de build-vlag van de host (gesteld in de entry van de host in config/daemon.js, zie hieronder); de handlercode is identiek.

14.3: Installatie

De daemon is een Node-service die buiten het Phlo-framework leeft. Voer het uit, wijs je reverse proxy erop aan, en dat is de hele realtime setup.

git clone https://github.com/q-ainl/phlo-daemon.git <daemon>
cd <daemon>
npm install

Het neemt een poort, de PHP-binaire en de hostmap:

// <daemon>/config.js
require('./phlo-daemon.js')(3001, '/usr/bin/php-zts', {
	'demo.example.nl': { app: '/var/www/demo/www/app.php', build: true },
})

Het derde argument is de hostmap: het koppelt elke Host aan zijn app.php pad en een build vlag, en wordt gedeclareerd in config/daemon.js. De daemon laadt het bij het opstarten, zodat het altijd weet welke hosts bestaan en of elke host een one-shot of pooled is. Een host zonder invoer faalt de dispatch, wat de upgrade faalt.

Voer het uit onder een procesbeheerder (systemd / pm2 / supervisord); de phlo-daemon README beschrijft het pm2-patroon en het /message brugcontract:

node <daemon>/config.js

Voor productie, stuur wss:// via je reverse proxy (Caddy, Nginx, FrankenPHP) naar 127.0.0.1:3001 voor het pad /websocket. Je app declareert dezelfde poort als de daemon constante in www/app.php:

phlo_app(
	app: __DIR__.'/../',
	daemon: 3001,
)

14.4: App hooks

In your app source you define four functions; Phlo's websocket resource calls them if they exist. Put them in a file like app.ws.phlo: do not name the file websocket.phlo, because that class name collides with the engine's websocket resource when it is loaded.

function wsConnect($wsHost, $wsToken, $wsSocket){
	// The app decides who hears what: here every screen of a signed-in user joins that user's channel.
	($user = %user->byToken($wsToken)) && wsJoin('user:'.$user->id, $wsSocket)
	return true
}

function wsAuth($wsHost, $wsToken, $wsSocket){
	$user = %user->byToken($wsToken)
	if (!$user) error('unauthorized')
	%session->user = $user
	return true
}

function wsReceive($wsHost, $wsToken, $wsSocket, ...$data){
	$type = $data['type'] ?? null
	if ($type === 'ping') return wsCast(wsTarget: 'socket:'.$wsSocket, pong: time())
	if ($type === 'chat.send') chat::send($data['text'], from: %session->user->id)
}

function wsClose($wsHost, $wsToken, $wsSocket){
	debug('ws close '.$wsSocket)
}
HookWhenNotes
wsAuthAt the handshake, before the socket is acceptedValidate $wsToken; raise an error to refuse the connection
wsConnectRight after the socket is acceptedSet-up (presence, logging); broadcast with wsCast()
wsReceiveFor every subsequent message (JSON-decoded and spread)Respond with wsCast(); printed lines stream back to the sender
wsCloseThe connection closesClean-up (presence); broadcast with wsCast()

$wsSocket is an opaque string identifier you can use to broadcast back to exactly this client.

The connection-context arguments are ws-prefixed by convention ($wsHost, $wsToken, $wsSocket), exactly like wsCast. This is not cosmetic: wsReceive spreads the JSON payload into named arguments (...$data), so an unprefixed $host/$token/$socket parameter would fatally collide with a payload that carries a host, token or socket key. Keep the prefix and your payload keys stay free.

14.5: Auth flow

De daemon authenticates tijdens de handshake, voordat hij de socket accepteert:

  1. De browser opent wss://<host>/websocket. De cookies voor die oorsprong, inclusief een token cookie, worden meegestuurd met het upgradeverzoek.
  2. De daemon leest de token cookie. Als deze ontbreekt, wordt de upgrade geweigerd met 401.
  3. De daemon voert websocket::auth($wsHost, $wsToken, $wsSocket) uit op zijn pool, wat jouw wsAuth aanroept.
  4. wsAuth valideert de token tegen %user, %session->token of een aangepaste lookup. Geef een foutmelding (error('unauthorized')) om te weigeren: een gegooide auth mislukt de upgrade. Bij succes opent de socket en wordt wsConnect uitgevoerd.

De token komt doorgaans van %user->token (per ingelogde gebruiker) of een API-sleutel, ingesteld als de token cookie wanneer de pagina wordt weergegeven. De browser stuurt deze automatisch mee tijdens de WS-handshake; de client stuurt geen aparte auth-bericht.

14.6: Broadcasting from PHP

wsCast() is a regular function (resource wsCast). It does a POST to the daemon's internal /message bridge, which pushes it on to the right sockets.

wsCast(wsTarget: 'all',                  toast: 'New message received')
wsCast(wsTarget: 'socket:'.$wsSocket,    path: '/inbox')
wsCast(wsTarget: 'token:'.$token,        inner: ['#count' => $newCount])
wsCast(wsTarget: 'channel:user:7',       toast: 'Your order shipped')

A channel holds the sockets the app put in it with wsJoin('user:7', $wsSocket) (or wsJoin('orders', wsToken: $token) for every screen of one token), usually in wsConnect where the app knows who connected. wsLeave() takes them out; a closed socket leaves by itself.

ArgumentDefaultMeaning
wsTarget'all''all', 'token:<id>', 'token:not:<id>', 'socket:<id>' or 'channel:<name>'
wsHosthostVhost the broadcast applies to (default: current host)
wsPortdaemon (constant from app config)The daemon's port
wsExceptnoneOne socket id to skip, whichever target selected it
...$datanoneNamed args become the payload, usually apply() commands

A token is a principal, not a connection: one user with two tabs is one token with two sockets. So token:not: is how you reach other users, and wsExcept is how you reach the sender's other screens. Pass the $wsSocket your hook was handed:

function wsReceive($wsHost, $wsToken, $wsSocket, ...$data){
	wsCast(wsTarget: 'token:'.$wsToken, wsExcept: $wsSocket, inner: $data['inner'])
}

Spell the payload out there; ...$data after a named argument is a fatal PHP error.

The payload is passed through to the client and applied to the DOM automatically by phlo.js: the same apply() protocol you know from async routes.

No retry, no dead-letter, no ACK. If the daemon is down, the POST fails silently. For guaranteed delivery (financial events): combine with a DB queue.

14.6.1: Channels

A cast to channel:<name> reaches the sockets the app put in that channel, so a channel is as private as the check before the join:

wsJoin('orders', $wsSocket)
wsJoin('user:7', wsToken: $token)
wsCast('channel:orders', inner: ['#total' => $total])

wsJoin(wsToken:) covers the sockets a token has now; a tab opened later joins through wsConnect. Behind a proxy that keeps the socket from opening, app.websocket.poll = 'orders/live' asks that path while the socket is down.

14.6.2: Live models

static objLive = true on a model casts that it changed on every write, and nothing about what. The CMS refreshes an open list, and each screen asks with its own rights. Any page can do the same with an app.mod.live of its own.

14.7: Client side

The client itself does nothing special. Add DOM/websocket to your resources in data/app.json:

{
	"resources": [..., "DOM/websocket", "wsCast"]
}

DOM/websocket injects a script that:

If you want to send from JS: app.websocket.send({type: 'chat.send', text: 'hi'}).

To react to the connection itself, subscribe rather than assign:

const off = app.websocket.on('connect', () => app.websocket.send({want: 'state'}), element)

app.websocket.connect = fn still works, but it is a single field: a second feature assigning it replaces the first without a word. on() takes as many subscribers as you like, fires straight away when the socket is already open (so a page that mounts late still gets its moment), drops a subscriber once the element you passed leaves the document, and returns an unsubscribe function. close and error work the same way.

14.8: Mini example: presence

Show "who is online" without polling.

function wsConnect($wsHost, $wsToken, $wsSocket){
	apcu_store(apcu_key("presence:$wsSocket"), time(), 3600)
	wsCast(wsTarget: 'all', inner: ['#online-count' => (string)presence_count()])
	return true
}

function wsClose($wsHost, $wsToken, $wsSocket){
	apcu_forget("presence:$wsSocket")
	wsCast(wsTarget: 'all', inner: ['#online-count' => (string)presence_count()])
}

function presence_count():int => iterator_count(new APCUIterator('/^'.preg_quote(apcu_key('presence:'), '/').'/'))

The server keeps no state; APCu counts the sockets. apcu_key() puts the app in front of the key, because APCu is shared by every app on the server, and apcu_forget() drops a key by its unscoped name. On a PHP restart the cache empties by itself, which is fine, because an empty presence is an acceptable degraded state.

14.9: Known limitations



Reference. The realtime and DOM resources are documented per node in the Manual, generated from the resource files so it never drifts from the code.

Laatst bijgewerkt op 05-11-2026

We gebruiken essentiële cookies om deze site te laten werken. Met uw toestemming gebruiken we ook analytics om de site te verbeteren.