Skip to main content

Streaming Responses

Beyond the standard res.json()/res.send()/res.end() methods, HttpResponse also supports writing a response incrementally — implemented identically on both engines:

MethodWhat it does
res.flushHeaders()Sends the status line and headers immediately, without ending the response
res.write(chunk)Flushes headers on first call if not already sent, then writes a chunk without ending
res.onAbort(callback)Fires if the client disconnects mid-stream

Server-Sent Events (SSE)

@Get('/events')
async streamEvents(@Response res: HttpResponse) {
res.setHeader('content-type', 'text/event-stream');
res.flushHeaders();

const interval = setInterval(() => {
res.write(`data: ${JSON.stringify({time: Date.now()})}\n\n`);
}, 1000);

res.onAbort(() => clearInterval(interval));
}

No next() call is needed once you've started streaming — the framework detects an in-progress stream and doesn't wait on it the way it would a normal handler.

Note that BaseStaticRoute does not use this — it reads each file fully into memory before responding. This page is for endpoints you write yourself that need to push data incrementally.