---
title: "Live data without wiring"
description: "How a LiveValue finds its query, and how the server knows whom to update."
url: "https://arkapi.emincanozcan.com/docs/sauce-live"
---

# Live data without wiring

How a LiveValue finds its query, and how the server knows whom to update.

## Subscribing to a function call

```ts
@State messages: LiveValue<Message[]> = new LiveValue(() => listMessages(id))
```

A LiveValue receives a function, not an endpoint name. So how does it know what to subscribe to?

It runs that function once in **capture mode** (`runtime/arkapi/src/main/ets/runtime.ets`). While capture is on, the generated `listMessages` does not send a request: it records its endpoint and arguments and returns a promise that never settles. The LiveValue reads what was recorded and subscribes with it.

```diagram
 LiveValue          generated client          socket
 ─────────          ────────────────          ──────
 capture on ──────► listMessages(7)
                    records "chat.listMessages", [7]
                    returns a promise that never settles
 capture off
 reads the record ──────────────────────────► {id: 3, name, args: [7]}
                                          ◄── {id: 3, data: [...]}
```

The subscription starts on the first read of `data` or `loaded`, not in the constructor. ArkUI only notices changes made after it has wrapped the object, and the constructor runs before that.

## One socket for the whole app

Every LiveValue and every `onX` listener shares one WebSocket per backend. If it drops, the library reopens it every second and sends all open subscriptions again. A reconnect within about 3 seconds is invisible: `connected` stays true and the screen keeps its data.

## Updates without saying what changed

You never tell ArkAPI which screens to refresh. After **anything that can change data** (a mutation, an HTTP route, an upload, a job, a device connecting or leaving), the server re-runs the open live queries (`cli/src/serve.ts`).

```diagram
 sendMessage() ┐
 upload        ├─► refresh ─► group by query + args ─► run once ─► diff ─► send
 job run       │      ▲                                  │
 device online ┘      └──── more changes meanwhile? ─────┘ one more round
```

What keeps that cheap:

- **Batching.** Changes in the same event-loop turn share one refresh. Changes during a refresh cause exactly one more.
- **Sharing.** Ten phones watching the same query with the same arguments cost one run, not ten.
- **Knowing when not to share.** While a query runs, ArkAPI notes whether it called `caller()`. If it did, the result depends on who asks, so it runs per device. You never mark this; reading `caller()` is the mark.
- **Silence.** A result that did not change is not sent.
- **Diffs.** A list that only gained items at the end or lost items at the start is sent as `{drop, append}`. A chat with 100 messages that gets one more sends one message, not 101.

## Presence for free

The server knows which devices have a socket open; `online()` returns them. Because a device connecting or leaving also triggers a refresh, an "online" dot computed from `online()` updates by itself.

## Events ride the same socket

`event<T>()` is for moments that are not state: someone is typing, a notification. `typing.send(devices, data)` pushes it only to those devices, over the socket they already have. Nothing is stored; a device that is offline misses it, by design.
