# Realtime

Stream record changes over Server-Sent Events on the same GET paths as JSON:API.

You may stream record changes with Server-Sent Events (SSE) on the same URIs as the [REST API](/docs/rest-api/). The path stays `/v1/{resource}` or `/v1/{resource}/{id}`. The `Accept` header chooses the response shape.

To open a collection stream:

```sh
curl -N http://127.0.0.1:3000/v1/posts \
  -H "Authorization: Bearer crt_…" \
  -H "Accept: text/event-stream"
```

| Accept | Response |
| --- | --- |
| omitted, `*/*`, or `application/vnd.api+json` | JSON:API document (unchanged) |
| `text/event-stream` (or preferred via `q`) | SSE stream |

Permissions, row scope, and field projection match a normal GET. List query filters shape the opening snapshot and which later lifecycle events are forwarded.

## Open a stream

For one record:

```sh
curl -N http://127.0.0.1:3000/v1/posts/<id> \
  -H "Authorization: Bearer crt_…" \
  -H "Accept: text/event-stream"
```

The response uses `Content-Type: text/event-stream` and `Cache-Control: no-cache`. Charter sends comment keepalives about every 15 seconds so proxies keep the connection open.

## Events

1. **`snapshot`**. First event. The `data:` field is the same JSON:API document a normal GET would return (collection with `meta` for lists, or one resource for retrieve).
2. **Lifecycle**. Later events use the lifecycle name as the SSE `event:` field (`posts.created`, `posts.updated`, `posts.replaced`, `posts.deleted`). The `data:` JSON matches [webhook](/docs/webhooks/) payloads (`event`, `resource`, `id`, `before`, `after`, `actor`, `trace`). The SSE `id:` field is the record UUID.

Collection streams receive events for that resource. Item streams receive events for that id only. Soft-deleted rows leave the stream after the `*.deleted` event.

When a change crosses your row scope or list filters, Charter still sends the lifecycle event if either `before` or `after` matches, and sets the out-of-scope image to `null`. That matches GET: you never see attribute values for a state your filters would hide.

> [!INFO]
> Reconnects start over with a new snapshot. Charter does not replay from `Last-Event-ID`. Streams are process-local: subscribe on the same `charter serve` / `charter dev` process that handles the writes you care about. Resource MCP writes on that process feed the same bus as REST.

## Browser clients

[`EventSource`](https://html.spec.whatwg.org/multipage/server-sent-events.html) sends `Accept: text/event-stream` by default. Pass the API key or access token the same way your app authenticates other `/v1` calls (for example a cookie session, or a fetch-based EventSource wrapper that can set `Authorization`).

## Related

- [REST API](/docs/rest-api/) for list filters and JSON:API documents
- [Webhooks](/docs/webhooks/) for outbound HTTP delivery of the same lifecycle events
- [OpenAPI](/docs/openapi/) documents `Accept` and `text/event-stream` on resource GETs
