# Live bars (SSE)

> The Server-Sent Events contract for live K-lines: snapshot, forming, closed and status frames.

Source: https://kcq.nebutra.com/docs/market-data/live-bars


<Callout>
  Generated from 

  [`packages/core/src/data/live/types.ts`](https://github.com/TsekaLuk/KLineChartQuant/blob/1aa5e22c61313a34d08878ad895e604e0d4a85e9/packages/core/src/data/live/types.ts)

   and 

  [`barsLive.ts`](https://github.com/TsekaLuk/KLineChartQuant/blob/1aa5e22c61313a34d08878ad895e604e0d4a85e9/packages/core/src/data/live/impl/barsLive.ts)

  .
</Callout>

A connector streams live K-lines to the chart over Server-Sent Events. One connection carries one series: a source, a symbol, a period and a bar aggregation. Changing any of them closes the stream and opens a new one.

Endpoint [#endpoint]

```http
GET /api/v1/market-data/sources/{sourceId}/stream?symbol=…&period=…&barAggregation=…&instrumentId=…
Accept: text/event-stream
```

| Query                       | Required |
| --------------------------- | -------- |
| <code>symbol</code>         | Yes      |
| <code>period</code>         | Yes      |
| <code>barAggregation</code> | Yes      |
| <code>instrumentId</code>   | —        |

`instrumentId` is the resolved instrument's stable id. It is sent only when known, so a bare code that exists on two exchanges still reaches the right one.

Frames [#frames]

Each SSE `data:` line is one JSON frame. Comment lines (`:` keep-alives) and empty data are ignored.

| `type`                | Fields                                                                                                           | Meaning                                                                                                                           |
| --------------------- | ---------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------- |
| <code>snapshot</code> | <code>symbol: string</code> <code>period: string</code> <code>bars: LiveBar\[]</code>                            | The full tail of the series. Sent first and after a reconnect; the chart writes it in one batch and drops any pending closed bar. |
| <code>forming</code>  | <code>symbol: string</code> <code>period: string</code> <code>bar: LiveBar</code>                                | The bar that is still forming. Written together with a pending closed bar as one atomic update.                                   |
| <code>closed</code>   | <code>symbol: string</code> <code>period: string</code> <code>bar: LiveBar</code>                                | A bar's final values. Held until the next forming bar arrives (or the stream stops), then written with it.                        |
| <code>status</code>   | <code>symbol: string</code> <code>period: string</code> <code>status: string</code> <code>detail?: string</code> | Connector state for the subscription, with optional detail. It does not change bars.                                              |

LiveBar [#livebar]

| Field                  | Type                | Required |
| ---------------------- | ------------------- | -------- |
| <code>timestamp</code> | <code>number</code> | Yes      |
| <code>open</code>      | <code>number</code> | Yes      |
| <code>high</code>      | <code>number</code> | Yes      |
| <code>low</code>       | <code>number</code> | Yes      |
| <code>close</code>     | <code>number</code> | Yes      |
| <code>volume</code>    | <code>number</code> | —        |
| <code>turnover</code>  | <code>number</code> | —        |

`timestamp` is UTC milliseconds. Missing `volume` and `turnover` are written as 0.

```text
data: {"type":"snapshot","symbol":"600519","period":"1min","bars":[…]}

data: {"type":"forming","symbol":"600519","period":"1min","bar":{"timestamp":…,"open":…,"high":…,"low":…,"close":…}}

data: {"type":"closed","symbol":"600519","period":"1min","bar":{…}}
```

Connection state [#connection-state]

The stream reports `connecting`, `connected`, `disconnected`. The browser's EventSource reconnects on its own; a connector can replay missed frames from `Last-Event-ID`.
