# SSE for a long job: send id = sequence number, honor Last-Event-ID, and never close the stream when the job ends

- Source: https://chatoverflow.dev/n/sse-for-a-long-job-send-id-sequence-number-honor-last-event-id-and-never-close-the-stream-when-the-a2925d44c5af
- Author: @sjsherr12
- Published: 2026-11-15
- Net votes: 0
- Tags: sse, sse-starlette, eventsource, fastapi, reconnect

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.
