# Live stream

Stream who is on a site right now, and every pageview and event as it lands, over Server-Sent Events.

The live stream sends the visitors on a site right now, then each pageview and event within a second of it landing. It is the feed behind the [Realtime](/docs/dashboard/realtime) page, for a script, an office screen or a chat bot.

`GET https://api.ultanimus.com/v1/sites/{id}/live`

`{id}` is the site ID, such as `ult_fxpitz4w7h`, or the site's domain. The key needs the `stats:read` permission, and the organization needs Insight or Foresight.

## Connect

The answer is a [Server-Sent Events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events) stream that stays open. Pass `-N` so curl prints each message as it arrives.

```bash
curl -N https://api.ultanimus.com/v1/sites/YOUR_SITE_ID/live \
  -H "Authorization: Bearer $API_KEY"
```

```text
event: snapshot
data: {"now":1792936825000,"minute":1792936800000,"views":[14,11,9,...],"named":{...},"visitors":[...],"events":[...]}

event: events
data: [{"at":1792936826250,"visitor":"7m2c0p4rt1","name":"signup","page":"/pricing",...}]

: ping
```

Keep the key on your server. A browser's `EventSource` cannot send an `Authorization` header, so a web page that shows the feed reads it through your server.

## Messages

- `snapshot` comes first, once. It holds the site as it was when you connected.
- `events` follows whenever something lands. It holds the pageviews and events of the last 250 ms, up to 200 in one message.
- A line that starts with `:` is a comment the server sends every 20 seconds, so proxies keep a quiet connection open. Skip it.

### snapshot

```json
{
  "now": 1792936825000,
  "minute": 1792936800000,
  "views": [14, 11, 9, 12, 16, 13, 10, 8, 11, 15, 17, 12, 9, 10, 13, 14, 12, 11, 9, 8, 12, 15, 13, 10, 11, 14, 16, 12, 10, 4],
  "named": {
    "signup": [0, 1, 0, 0, 0, 2, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1]
  },
  "visitors": [
    { "id": "3k9x2f1q8z", "seen": 1792936821000, "page": "/pricing", "source": "google", "country": "GB", "city": "London", "device": "desktop", "browser": "chrome", "lat": 51.5, "lon": -0.1 }
  ],
  "events": [
    { "at": 1792936821000, "visitor": "3k9x2f1q8z", "name": "pageview", "page": "/pricing", "source": "google", "country": "GB", "city": "London", "device": "desktop", "browser": "chrome", "lat": 51.5, "lon": -0.1, "landing": true }
  ]
}
```

- Times are Unix milliseconds. `now` is the server's clock when it sent the snapshot, so you can age visitors even when your own clock is off.
- `views` holds the pageviews of each of the last 30 minutes, oldest first. The last number is the current minute, which started at `minute`.
- `named` holds the same 30 counts for each custom event sent in that time, keyed by event name: the 20 names with the most events. Add each event from `events` to its name's last number, and shift every list along with `views` when a minute ends.
- `visitors` are the visitors active in the last 5 minutes, up to 2,000, the most recent first. `seen` is the time of their last event, and the other fields describe that event.
- `events` are the 40 latest pageviews and events of the last 30 minutes, newest first.

### events

```json
[
  { "at": 1792936826250, "visitor": "7m2c0p4rt1", "name": "signup", "page": "/pricing", "source": "chatgpt", "country": "DE", "city": "Berlin", "device": "mobile", "browser": "safari", "lat": 52.5, "lon": 13.4 }
]
```

- `name` is `pageview` or the name of a [custom event](/docs/tracking/custom-events), such as `signup`.
- `visitor` links the events of one visitor through the day. It changes at midnight UTC and says nothing about who the person is.
- `source` is where the visit came from, such as `google` or `chatgpt`.
- `country` is a two-letter code. `lat` and `lon` place the visitor at their city, rounded to about 11 km, and are left out when the place is unknown.
- `landing` is `true` on the pageview that started a visit, and left out on every other event. In a snapshot it is a best guess, made from the 40 events the snapshot holds.
- `target` is the link the visitor followed, as its host and path, such as `github.com/acme/app`. Only [`outbound_link` and `file_download`](/docs/tracking/links-and-downloads) events carry it. The stream sends no other property of an event.

## Keep a live view

To show the same numbers as the Realtime page:

1. Load `snapshot` into your state. After a reconnect, replace your state with the new one.
2. Add each event in `events` to the feed, and set its visitor's `seen` to its `at`. Count a pageview in the minute it landed.
3. Drop a visitor once their `seen` is 5 minutes old.

An event can arrive twice around a connect, once in `snapshot` and once in `events`. Skip an event when you already hold one with the same `visitor`, `at` and `name`.

## When the stream ends

The server ends each stream after 10 minutes. It ends one sooner when a deploy restarts the server, or when your client stops reading and falls 256 events behind. Connect again, and the new `snapshot` brings the numbers back up to date.

## Limits and errors

A key may hold 4 streams open at once and open 30 a minute. An error arrives as the usual [JSON envelope](/docs/api/overview#responses), before the stream starts.

| Status | Code | When |
|---|---|---|
| 403 | `ULT-1203` | The organization is on Glance. |
| 403 | `ULT-1221` | The key lacks `stats:read`. |
| 429 | `ULT-1902` | The key has 4 streams open. Close one first. |
| 429 | `ULT-1902` | The key opened 30 streams in the last minute. `Retry-After` says how many seconds to wait. |
| 503 | `ULT-9003` | Realtime is busy on the server. Try again in a few seconds. |
