← All lessons/Real-Time
40
Real-Time

Per-Agent SSE Stream

Stream every new record written to a specific agent in real time.

Prerequisite: Lesson 5 complete

What you'll learn

  • GET /v1/agents/:id/stream — open SSE connection
  • Each written record emitted as event: record
  • Stream format: event: record\ndata: {content_hash, ts_hlc, payload}
  • Last-Event-ID header for reconnect and replay
  • EventSource API in browsers — automatic reconnect
Challenge

Open SSE stream in one terminal. Write 10 records in a loop in another. Confirm all 10 appear in order.

Interactive Walkthrough

What you'll learn

Stream every new record written to a specific agent in real time.

Open the stream

# Streams all new records written to 'orders' agent
curl -N "http://localhost:7475/v1/agents/orders/stream" \
  -H "Authorization: Bearer spx_root_YOUR_ROOT_KEY"

Keep this running. In another terminal, write a record:

curl -s -X POST http://localhost:7475/v1/agents/orders/write \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer spx_root_YOUR_ROOT_KEY" \
  -d '{"payload": {"amount": 99.00, "status": "paid"}}' \
  | python3 -m json.tool

You'll see the record appear in the SSE stream terminal immediately.

Stream format

` event: record data: {"content_hash":"b3a7c2...","ts_hlc":1751900000000000,"payload":{"amount":99.00,"status":"paid"}}

event: record data: {"content_hash":"d4e5f6...","ts_hlc":1751900065536000,"payload":{"amount":149.00,"status":"pending"}} `

Reconnect and replay

SSE clients automatically reconnect on disconnect. Pass Last-Event-ID to resume from where you left off:

curl -N "http://localhost:7475/v1/agents/orders/stream" \
  -H "Authorization: Bearer spx_root_YOUR_ROOT_KEY" \
  -H "Last-Event-ID: 1751900000000000"

Challenge

Open an SSE stream in a terminal. Write 10 records using a loop. Confirm all 10 appear in the stream in order.

---

← Previous
Lesson 39: Mutants (AI Schema Evolution)
Next →
Lesson 41: Global Broadcast Bus & Publications