Streams and server-sent events
A declaration's response kind tells the server what a handler may return instead of a JSON body. The handler is still an @Endpoint method, the code is the same on every adapter, and it never touches the native reply object.
An async iterable yields strings or Uint8Arrays. A Node Readable is one.
These responses are not validated at run time; there is no schema to validate against. Set another content type, and any other header, with ctx.reply.header(). A Blob that has a type uses it as the content type. Anything else, such as a web Response, is answered as a 500.
How these endpoints are declared, and how a client consumes them, is covered by the Builder guide Downloads and streams. The examples below implement three such declarations:
Text
Binary downloads
Errors work as on any endpoint: the NotFoundException is answered as a 404 problem response, and an errorSchema on the declaration allows reply(404, body). See Errors.
To send a file from disk without loading it, return a stream of it:
The file is closed when the download ends, however it ends. A file that cannot be opened is an ordinary error response.
Streaming
The simplest streaming handler is an async generator method. Each value it yields is written as one chunk: a string or a Uint8Array.
thisinside the generator is the controller, so injected services are available.- Headers must be set before the first
yield. Once a chunk has been written the status and headers are on the wire. - A stream whose content type is set to
text/event-streamis not held back until its first chunk. Its headers are those set before the generator first waits for something. - A handler may also return any
AsyncIterableor a webReadableStreamit got from elsewhere, such as the body of an upstreamfetch.
When a stream ends early
The request owns what its handler returned. Whatever becomes of the response, the source is closed: the client disconnects, also before the first chunk, the request is a HEAD, an interceptor answers with something else, or the application closes. A generator runs its finally blocks, an iterator's return() is called, a web stream is cancelled and a Node stream destroyed. ctx.onFinish callbacks run after that, and request-scoped services are disposed last, so a finally can still use them:
A generator can only be closed at a yield. One that waits for something else is closed when it gets there, and after a second the rest of the request is released without it. ctx.signal aborts when the client disconnects; pass it to whatever produces the data so that the wait ends too.
Errors while streaming
An error before the first chunk is an ordinary error response: the generator above can throw NotFoundException on its first line and the client gets a 404. So is an error that a returned stream fails with before it has produced anything.
After the first chunk the status has been sent, so there is nothing left to change. The error is logged with the route, unless the client is gone, and the connection is closed without a clean end, which the client sees as a failed read. When the consumer must know why, make the failure part of your stream's format, for example a final line or event that says so. A chunk that is neither a string nor a Uint8Array is such an error.
Server-sent events
A response: 'stream' endpoint that returns sseStream(source) answers with text/event-stream. source is an async iterable of events; @navios/server writes the framing and sets cache-control: no-cache unless the handler set one.
The response is decided when the handler returns, before the source runs, because the client must not wait for the first event to learn that it is connected. An error the source throws, even before its first event, is logged and closes the connection. Do what may refuse the request, such as loading the record the events belong to, before sseStream(). Bun sends headers together with the first bytes of a body, so the Bun adapter opens the stream with an empty comment, which clients ignore.
On the client this pairs with the Builder Server-Sent Events module. Events defined there with defineEvent are what sse() takes on the server, so both sides share the event name and the payload schema:
The client receives:
There are two ways to produce an event:
sse() accepts anything with config: { eventName, payloadSchema? }, so it does not require @navios/builder. The payload schema must validate synchronously. A payload that fails it throws inside the generator, which ends the stream.
Guards, interceptors and per-request state work for streamed responses as for any other. Request values and request-scoped services stay usable while the stream is being sent, and are released when it has been closed.
On Bun, a connection on which nothing has been sent for serve.idleTimeout (10 seconds by default) is closed. The adapter turns that off for event streams only. Raise the option for other streams that may be quiet for longer, or send something in between; see Idle connections.
A reverse proxy that buffers responses delays events until its buffer fills. @navios/server sends x-accel-buffering: no with every event stream, which nginx honours; other proxies need buffering turned off for the route.