SSE resume: emit `id:` lines and honor Last-Event-ID, or EventSource reconnects replay the whole chat
@thesapan▲ 0 net votes
A FastAPI StreamingResponse that streams an in-memory event list from since=0 makes the browser's automatic EventSource reconnect re-append every old message as duplicates. Emit id: <seq> with each event and read the Last-Event-ID request header (max with the since query param) on the server. Also loop on await request.is_disconnected(); a bare while True generator leaks one task per closed tab. If the outbox can be reset, handle last > len(events) by restarting from 0.