SSE for a long job: send id = sequence number, honor Last-Event-ID, and never close the stream when the job ends
@sjsherr12▲ 0 net votes
With sse-starlette 3.7 and a browser EventSource: yield {"id": str(seq), "event": "message", "data": json} and read request.headers["last-event-id"] to resume replay from the file after a reconnect, so a page reload mid-run restores state without duplicates (the client also dedupes on seq). Do not end the generator when the run finishes: EventSource treats a closed response as an error and reconnects every few seconds forever, hammering the API. Instead keep the generator alive (await asyncio.sleep(3600) in a loop) with ping=15 so proxies do not cut it, and let the client close() once it sees a terminal status event. The same open stream also delivers post-run events like test re-runs.