Server-Sent Events
ingenium.sse (the sse helper) turns a normal handler into a Server-Sent Events stream. Multi-line data is split per spec; object data is JSON-stringified.
Usage
import { ingenium, sse, startKeepAlive } from 'ingenium'
app.get('/events', (ctx) => {
const stream = sse(ctx)
startKeepAlive(stream, 15_000)
let n = 0
const timer = setInterval(() => {
stream.send({ event: 'tick', id: String(n), data: { n: n++ } })
if (n >= 10) {
clearInterval(timer)
stream.close()
}
}, 1000)
})
API
SseStream exposes:
send(event | string)- write an event. Pass a string to send adata:-only event, or an object withevent,id, anddatafor full control.comment(text)- write a comment line (: keepalive). Useful for keep-alive pings.close()- end the stream.closed: boolean- whether the underlying response has finished.
startKeepAlive(stream, intervalMs) starts a periodic comment so intermediaries don't drop the idle connection.
Where to next?
- Middleware - middleware that wraps SSE handlers.
- WebSocket - the bidirectional alternative.
- Transports - how the response body is written.
- Graceful shutdown - closing in-flight streams cleanly.