# Live streams (SSE)

| Stream                          | Endpoint                                   |
| ------------------------------- | ------------------------------------------ |
| Deployment or tool metrics      | `GET /metrics/v1/resources/live`           |
| Environment metrics             | `GET /metrics/v1/namespace/live`           |
| Deployment status               | `GET /deployment/v1/live/deployment/stream` |
| Tool status                     | `GET /deployment/v1/live/tools/stream`     |

Each stream sends an `update` event as soon as you connect, then one every `interval` seconds (minimum 15). A keep-alive comment arrives every 20 seconds, and errors arrive as `event: error`.

```text
event: update
data: {"ts":1730800000,"identity":{"namespace":"env-k3x9q2m7","resourceType":"cpu"},"values":{"usage":42.5,"request":250,"limit":500}}
```

Streams need the `Authorization` header. The browser `EventSource` API can't send headers, so use `fetch`:

<CodeTabs>

```js title="JavaScript"
async function stream(url, token, onEvent, signal) {
  const res = await fetch(url, {
    headers: { Authorization: `Bearer ${token}`, Accept: "text/event-stream" },
    signal,
  });
  const reader = res.body.pipeThrough(new TextDecoderStream()).getReader();
  let buf = "";
  for (;;) {
    const { value, done } = await reader.read();
    if (done) break;
    buf += value;
    let i;
    while ((i = buf.indexOf("\n\n")) !== -1) {
      const frame = buf.slice(0, i);
      buf = buf.slice(i + 2);
      const event = /^event: (.*)$/m.exec(frame)?.[1] ?? "message";
      const data = /^data: (.*)$/m.exec(frame)?.[1];
      if (data) onEvent(event, JSON.parse(data));
    }
  }
}
```

```shell title="cURL"
curl -N "$NIMBUZ_API/metrics/v1/namespace/live?namespace=$ENV_ID&resourceType=all&interval=15" \
  -H "Authorization: Bearer $NIMBUZ_TOKEN"
```

</CodeTabs>

If the connection drops, reconnect after about 5 seconds. If you get `401`, log in again first.
