Server-Sent Events are just an HTTP response with Content-Type: text/event-stream that is never closed.
The key: disable buffering, send data: followed by a blank line, then flush(). Detect a dropped connection with connection_aborted() so the loop stops.