EventSource.
In Bun, you can implement an SSE endpoint by returning a Response whose body is a streaming source and setting the Content-Type header to text/event-stream.
Bun.serve closes idle connections after 10 seconds by default. A quiet SSE stream counts as idle, so the
examples below call server.timeout(req, 0) to disable the timeout for the stream. See
idleTimeout for details.Using an async generator
In Bun,new Response accepts an async generator function directly. This is usually the simplest way to write an SSE endpoint — each yield flushes a chunk to the client, and if the client disconnects, the generator’s finally block runs so you can clean up.
Using a ReadableStream
If your events originate from callbacks — message brokers, timers, external pushes — rather than a linear await flow, a ReadableStream often fits better. When the client disconnects, Bun calls the stream’s cancel() method automatically, so you can release any resources you set up in start().