Client↔Host communication patterns for Shell-mediated Apps
Research for Shell bridge + dedicated App workers (see root CONTEXT.md).
Question: what patterns fit Apps whose browser logic is client Wasm with no ambient network, talking only through Shell glue?
Claims cite primary sources (MDN, Sandstorm docs).
Locked product context (already decided): Shell owns DOM; client Wasm in a dedicated worker per open App; Shell performs Host HTTP with HttpOnly session; UI patches/named events cross the Shell↔App bridge; Dataset / inter-App events are Host-mediated with stable event ids (at-least-once).
Executive recommendation (for grilling)
Treat communication as two hops:
- App worker ↔ Shell — always
postMessage/MessagePort(structured clone). This is the only path App Wasm should see. - Shell ↔ Host — Shell-owned network: prefer HTTP request/response RPC for calls the App initiates, plus one Host→Shell push channel (SSE or WebSocket) for Dataset / inter-App / capability wakes. Shell fans events into the right worker.
Do not give App workers raw WebSocket / EventSource / fetch. That would reintroduce ambient network and session-cookie risks already ruled out.
Pattern matrix
| Pattern | Direction | Who holds the socket? | Fit for our locks | Main trade-offs |
|---|---|---|---|---|
Worker postMessage RPC |
Bidirectional in-process | N/A (same origin page↔worker) | Required App↔Shell hop | Structured clone cost; must design request ids / correlation |
| MessageChannel ports | Bidirectional dedicated pipe | N/A | Nice for isolating one App’s channel from Shell bus noise | Extra setup; still no Host I/O |
| HTTP fetch RPC | Client→server (+ response) | Shell only | Natural for “call Host API / return result” | No server push alone; pair with a push channel |
SSE (EventSource) |
Server→client stream | Shell only | Strong for Host→Shell event fan-out; auto-reconnect; id field maps well to our event ids |
One-way; connection limits without HTTP/2; UTF-8 text |
| WebSocket | Bidirectional | Shell only if used | One connection for RPC+push | App must not own it; classic WS lacks backpressure (MDN); reconnect is app-defined |
| Long poll | Server→client via repeated HTTP | Shell only | Fallback where SSE/WS blocked | Latency/overhead; still Shell-owned |
| WebTransport | Advanced multiplexed | Shell only (future) | Mention as later option | Complexity / uneven support (MDN WebSockets page) |
Hop 1: App worker ↔ Shell
Worker messaging
Dedicated workers communicate with their creator via Worker.postMessage / DedicatedWorkerGlobalScope.postMessage, using the structured clone algorithm; optional transfer of ArrayBuffer, MessagePort, etc. (MDN Worker.postMessage; Transferable objects).
Lesson: Map UI patch diffs, named UI events, and Host RPC envelopes onto a small message schema with correlation ids. Large binary blobs should use transferables, not JSON stringify.
MessageChannel
The Channel Messaging API creates a two-port pipe transferable into a worker (MDN Channel Messaging API). Useful so each App install gets an exclusive port rather than multiplexing everything on the worker’s default onmessage.
Lesson: Prefer one MessagePort per App install owned by Shell glue; Wasm only sees bridge imports that eventually postMessage on that port.
Hop 2: Shell ↔ Host
HTTP request/response (RPC)
Shell uses credentialed fetch (HttpOnly cookie) to call Host APIs on behalf of an install id. Matches “Apps don’t see ids/URLs; Shell routes” (Decide App instance identity.
Pros: Simple mental model; easy auth; works with ordinary HTTP middleware; good for mutations and reads.
Cons: No push; polling is wasteful for Dataset change / inter-App events.
Server-Sent Events
EventSource opens a one-way server→browser stream (text/event-stream). Supports named events, data, id (last-event-id / resume), and retry; browser auto-reconnects after close (MDN Using server-sent events).
Pros: Excellent match for Host→Shell push of events that already have stable event ids; reconnect semantics documented; Shell multiplexes into workers.
Cons: Client cannot send on the SSE socket (use HTTP RPC for uplink); without HTTP/2, browsers historically limit concurrent SSE connections (~6 per browser+domain across tabs) (MDN warning); text/UTF-8 oriented.
Lesson for multi-tab: Prefer one SSE (or WS) per browser tab Shell, not per App worker — Shell demuxes by install id. Use HTTP/2 on the Instance to raise stream limits.
WebSockets
Bidirectional browser↔server messages; available in workers, but must not be handed to App Wasm under our security bar. Classic WebSocket does not support backpressure (buffers can grow / burn CPU); WebSocketStream adds streams/backpressure but is non-standard / limited engine support; WebTransport is positioned as a richer successor (MDN WebSocket API).
Pros: Single channel for uplink+downlink; binary friendly.
Cons: Reconnect/backoff is custom; open sockets can inhibit bfcache (MDN); easier to accidentally expose to Apps; backpressure footguns.
Lesson: If chosen, keep WS inside Shell glue as an implementation of the same logical RPC+event bus Apps already see via bridge imports.
Long polling
Repeated HTTP requests waiting for Host events. Universal fallback; higher overhead. Still Shell-only.
Mapping to product events
| Product need | Suggested transport role |
|---|---|
| UI patch / named UI event (App↔Shell) | Worker postMessage |
| App calls Host Dataset API | Shell → Host HTTP RPC |
| Host Dataset / inter-App event (at-least-once, stable id) | Host → Shell SSE (or WS), then Shell → worker postMessage |
| Capability wake (webhook result, etc.) | Same as Host→Shell push |
| Reconnect after tab sleep | SSE auto-retry + Last-Event-ID, or WS app reconnect; Shell re-attaches workers |
Inter-App / Dataset events already require stable event ids and idempotent handlers (Decide inter-App event and notification model) — SSE’s id field is a natural wire carrier; WS frames should carry the same id in the envelope.
Product prior: Sandstorm
Sandstorm mediates browser↔platform with postMessage to the parent frame (e.g. Powerbox requests with rpcId, responses with tokens) (Powerbox docs; grain.capnp comments). HTTP APIs are Host-gated on separate API hosts with CSP sandbox and token auth, not ambient cookies (Exporting HTTP APIs).
Lesson: Platform owns the wire and the parent/Shell bridge; apps speak a narrow RPC vocabulary. Our single-origin Shell+worker model is a different isolation choice, but the mediated bridge pattern still applies.
Directions for grilling
- Lock logical protocol: correlated RPC + event envelopes with stable ids — independent of WS vs SSE.
- Lock App sees only bridge imports; Shell owns all Host sockets.
- Prefer SSE (or WS) × 1 per Shell tab + HTTP RPC, not per-App sockets.
- Specify reconnect: replay from last event id; workers may miss events while dead — Host/Shell responsibility to resume or App resync via Dataset read.
- Defer WebTransport unless household scale demands it.