4: Socket monitoring
Because every connection passes through your hooks, you can see and manage sockets with ordinary Phlo models, no special tooling required.
4.1: Presence
Track who is connected by writing a row in wsConnect and deleting it in wsClose:
function wsConnect($wsHost, $wsToken, $wsSocket){
type_presence::create(socket: $wsSocket, token: substr((string)$wsToken, 0, 8), since: time())
return true
}
function wsClose($wsHost, $wsToken, $wsSocket){
type_presence::delete('socket=?', $wsSocket)
}
The live count is then just count(type_presence::records()), which you can broadcast to everyone on each change.
4.2: A connect/disconnect feed
Append every lifecycle event to a log model and you have an audit trail and a live feed. The demo's /monitor page renders the log and updates the online count and the feed through wsCast as sockets come and go.
| Want | Use |
|---|---|
| Online count | count(type_presence::records()) |
| Per-user sockets | filter presence by token |
| Event history | a wslog model, newest first |
| Targeted push | wsCast(wsTarget: $socketOrToken, ...) |
4.3: Limitations
phloWS is one broker process per app. It boots a handler per event, so there is a small per-event cost compared to a process that holds state in memory; in exchange you get the same stateless model as the rest of Phlo and no extra language. For very high-frequency streaming (many events per second per socket) a dedicated streaming server may fit better.