Skip to content

The wire protocol

You never write this protocol. The compiler and the mount derive both ends of it. This page documents it anyway, because auditable transport is part of situ's contract: everything below is visible in your browser's network tab.

What a page loads

GET  /todos                  the rendered page, containing:
                               <script id="siting-signals" type="application/json">
                                 {"local": {"draft": ""}, "url": ["filter"], "sets": []}
                               </script>
GET  /static/_rt.min.js?v=…  the shared runtime shim (about 7 KB gzipped, shared by every situ app)
GET  /todos/island.js        this mount's generated island (typically 5–12 KB, no-cache, with an ETag)

The runtime's v is a hash of the file, so the URL changes when situ does and can be cached for a year. The island is asked for again on each load: an unchanged one answers 304 Not Modified with no body.

The bootstrap seeds Local defaults (Local[set]s rehydrate to real JS Sets), lists the Url signal names and, for live mounts, the feed/region URLs.

The command round trip

POST /todos/cmd/add                      ← derived: one route per server handler
     X-Siting: 1
     Content-Type: application/x-www-form-urlencoded
     body: draft=Buy+milk&filter=all     ← form fields + riding local signals + current url params

  ←  200 text/html
     body: <the region's re-rendered inner HTML>
     X-Siting-Signals: {"draft": ""}     ← optional client-signal patch

On the client, in order: drop all region-scoped subscriptions → innerHTML-swap the region → apply the signal patch → re-bind binders inside the region. On failure (non-2xx or network error): log, roll back any optimistic apply, leave the region untouched.

An error answer has the same JSON body on every adapter: {"status_code": 404, "detail": "no server command 'nope'"}. A command POST without the X-Siting header is a 403, a command the page has no route for is a 404, a method other than POST on a command route is a 405. When the command raises a CommandError, the answer has that error's status. On Flask, an error raised by the host app outside the mount's routes, such as a body over the app's MAX_CONTENT_LENGTH, keeps Flask's own page.

  • Path args: a call's positional arguments become path segments (toggle(t.id) → /cmd/toggle/{id}, an int route).
  • Sets cross as JSON arrays. The bulk-action idiom: the selection rides the command; the server updates only ids that still exist.
  • In flight: the invoking control is disabled and the region aria-busy, so no request double-POSTs.

Region updates are coarse: the server re-renders one data-region from fresh context() and swaps it. There is no virtual DOM and no HTML-morphing library; the one keyed reconciler in the system is the client :each list engine (keyed by row id, focus-preserving, nested- and tree-capable). The demo programme found region-level coarseness right everywhere except broadcast streaming, which is what the feed is for.

Live updates (SSE)

mount_component(..., live=True) adds two routes and a hub:

GET /poll/feed      an SSE stream; emits "dirty" whenever any client's command mutates state
GET /poll/region    the region's HTML, re-rendered for the current query string

Each client keeps an EventSource on the feed; on dirty it re-fetches /region?{search} and swaps. A vote in one browser updates every connected peer (demo /poll, verified across two Chromium contexts).

Scope

The hub is in-process (asyncio queues): fine for one server process, while horizontal scaling needs a real broker behind the same interface. The live-refetch is the labelled stand-in for the reserved Synced site; a local-first replica is future work.

Windowed data

mount_component(..., window_template=...) adds:

GET /grid/window?offset=1200&count=40    positioned row HTML for the visible window

used by the :virtual binder. Scroll position stays a client signal; the row window is fetched and placed with a translate. The demo (/data-grid) renders 10,000 rows with ~40 in the DOM.

The reverse channel

X-Siting-Signals is the one server→client channel: a JSON object of signal patches attached to a command response. It holds the reset(...) defaults and signal(name, value) pushes from your handlers, applied atomically before re-bind so binders never observe a half-applied patch.

A patch small enough travels in the header. Proxies and browsers limit response headers, so a patch over 4 KB travels instead as the first line of the response body, with X-Siting-Signals: @body. Nothing changes in your code.

Seeding a Local from the server

A list that the server owns and the browser renders and filters is a Local signal that context() seeds:

from situ import seed


async def context(service, view):
    return {"doc": view["doc"], **seed(rows=await service.rows(view["doc"]))}

The page starts with the value, which comes back after every command, so a command only has to change the data. A signal() or reset() from the command itself wins over the seed. Under live=True, a peer's refresh brings it back too. Rows are keyed by id, so a list in the <header> is updated in place, and forms open in a row keep what is typed in them, unless their inputs use :bind.

A value that JSON cannot encode (NaN, a datetime, an object) is an error naming the signal.

A form outside the region is left as it is after a command. Add data-reset for it to come back empty on success, as a form in the region does with the re-render.