3: 使用 wsCast 进行广播

wsCast 是处理程序向客户端推送的方式。它向 Phlo Realtime 发送消息,Phlo Realtime 将其分发到目标套接字,每个浏览器通过与您的 async routes 已经使用的相同 phlo.js apply 管道来应用它。

3.1: Targets

wsCast(wsTarget: 'all', inner: ['#online' => (string)$count])
wsCast(wsTarget: 'socket:'.$wsSocket, toast: 'Just for you')

Everything after the target is an ordinary apply instruction: inner, outer, prepend, append, toast, scroll, and so on. The client treats a cast exactly like the response to a form, so the same view-update code works for both transports.

3.2: Leaving the sender out

A token identifies a principal, not a connection. One user with two tabs, or one till with a customer display beside it, is one token holding several sockets. That is the point of token targeting, and it is also why token:not: cannot help you skip a sender: it drops every socket of that token, including the other screens you were trying to reach.

wsExcept names one socket to skip, whichever target picked it up:

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

Every other screen of this token gets the message; the one that sent it does not. Use it whenever clients relay through the server to their peers, which is the usual shape for chat, typing indicators and second screens.

The exclusion is a separate argument rather than a target value, because the daemon has no idea who "me" is: a cast is its own HTTP call, unconnected to the socket that triggered your handler. The hook is handed $wsSocket, so the hook is the only place that can say.

Spell the payload out when you pass wsExcept. ...$data after a named argument is a fatal PHP error.

3.3: wsCast never throws

wsCast makes an HTTP call to Phlo Realtime and returns how many connections it reached. Without a running daemon that is 0: it never throws, so a cast cannot break the request that sends it, and a Phlo Realtime app degrades to a non-realtime app by itself. A wrapper with a try/catch of your own is not needed.

$reached = wsCast(wsTarget: 'all', inner: ['#online' => (string)$count])

3.4: 浏览器客户端

加载 DOM/websocket 资源会发送一个小型客户端。它 仅 在页面主体具有 wss 类时打开一个套接字,自动重新连接,并应用传入的转换。要为页面启用实时功能,请将应用程序置于该模式(演示中设置 prop options = 'wss');如果不启用,则该页面就是一个普通的 Phlo 页面。

3.5: Mirroring state between screens

With DOM/store loaded, a client can publish part of its own state instead of hand-rolling messages. Declare the path once:

app.sync('basket')            // over the socket, coalesced per 200 ms
app.sync('draft', {post: 'api/draft', delay: 1000})

Every change under that path now leaves as {sync: {basket: <value>}}. Changes are coalesced, so a burst of edits sends one message with the final value, not one per keystroke.

The server decides who may see it. A relay that hands the value to the other screens of the same principal is four lines:

function wsReceive($wsHost, $wsToken, $wsSocket, ...$data){
	if (!isset($data['sync'])) return
	wsCast(wsTarget: 'token:'.$wsToken, wsExcept: $wsSocket, sync: $data['sync'])
}

Nothing is relayed that you do not relay yourself: pick the paths you accept rather than passing the payload through unread, the same as with any other client input.

On the receiving side sync is an ordinary apply command, so it needs no code. It differs from store in one way that matters: a value applied through sync is not published again. Without that, two screens that both publish the same path would bounce it back and forth forever.

Two more things worth knowing:

最近更新于 2026年11月5日

我们使用必要的cookie来使该网站正常工作。在您的许可下,我们还使用分析工具来改善网站。