الوقت الحقيقي
Server-sent events for a tracking session, with resumable delivery. Understand reconnects before you rely on it.
GET /v1/tracking/sessions/{session_id}/stream returns text/event-stream. It authenticates with a tracking key in Authorization: Bearer, the same as every other tracking read.
shell
curl -N "https://api.bardiyamap.com/v1/tracking/sessions/ses_01HX9Z6Q3M/stream" \
-H "Authorization: Bearer $BARDIYA_TRACKING_KEY" \
-H "Accept: text/event-stream"Reconnecting
Each event carries an id. Send it back in Last-Event-ID when you reconnect and the stream resumes after that event instead of replaying from the beginning. This is what makes a mobile client on a flaky network workable.
ts
let lastEventId: string | undefined;
async function connect() {
const response = await fetch(streamUrl, {
headers: {
Authorization: `Bearer ${trackingKey}`,
Accept: "text/event-stream",
...(lastEventId ? { "Last-Event-ID": lastEventId } : {}),
},
});
if (!response.ok || !response.body) throw new Error(`stream ${response.status}`);
const reader = response.body.pipeThrough(new TextDecoderStream()).getReader();
let buffer = "";
for (;;) {
const { value, done } = await reader.read();
if (done) break;
buffer += value;
for (const frame of buffer.split("\n\n").slice(0, -1)) {
buffer = buffer.slice(frame.length + 2);
const id = /^id: (.*)$/m.exec(frame)?.[1];
if (id) lastEventId = id;
// handle the data: line here
}
}
}Practical limits
- A stream holds a connection open. One stream per session, per consumer: fan out on your side rather than opening one stream per viewer.
- Connections are subject to the infrastructure ceilings. Reconnect with backoff and jitter, never in a tight loop.
- Proxies buffer responses. Disable buffering for the stream route, and do not put a compressing proxy in front of it.
- A subscription stream also exists for the subscription API and authenticates with the same tracking key. It is not part of the published OpenAPI contract, so treat it as an advanced surface and confirm its frames against a live environment before depending on it.
