InputStream to read or an OutputStream to write. A
stream that the handler returns is a value in its result. When the handler returns or throws, the
SDK closes, aborts, or cancels each stream that the handler received. See
Manage the Stream lifecycle.
To call functions with streams, read streams, and create pipes, see Using iii /
Streaming. For how a stream works, see Understanding iii /
Streaming. To move code from the channel API, see Upgrading from
0.26.x.
Receive a stream
When the caller sends a stream in a field of the payload, the handler receives anInputStream in
that field. The payload does not contain the data. The handler reads the data from the stream.
A stream has one reader, so read each InputStream one time only. For the read operations, see
Read a stream from a result.
- Node / TypeScript
- Python
- Rust
- Go
InputStream that the handler started to read and did not finish. See
Manage the Stream lifecycle.
Return a stream
To return data as a stream, put the stream in the result. The caller receives anInputStream in
the same field. The handler’s SDK sends the result first, and then writes the data into the pipe.
The SDK sends the same values as streams in a result and in a payload. For these values, see
Send a stream in a call.
- Node / TypeScript
- Python
- Rust
- Go
Write to a received OutputStream
A caller can pass a sink, which is a destination of its own for the data. The handler receives the sink as anOutputStream and writes the data to it. A sink receives bytes only, so write the data
with write. The caller’s call resolves after the sink has received all the data.
- Node / TypeScript
- Python
- Rust
- Go
write fails with STREAM_CLOSED. The sink can
receive no more data, so let the error stop the handler.
Send messages
AnOutputStream carries bytes and messages. write sends bytes. send serializes one value to
JSON and sends it as one message, in one text frame. Use a message for a value that is not part of
the byte data.
One stream can carry both. For example, a route that the HTTP worker calls sends the status and the
headers of its response as messages, and the body as bytes. For the two types of frames, see
Bytes and messages.
- Node / TypeScript
- Python
- Rust
- Go
response. See
Calls through an observer.
Forward a stream
A handler can pass a stream that it received to another function. Put the stream in the payload of the other call, and do not read it first. The SDK then sends the ref of the stream, so the data does not go through the worker that forwards it.- Node / TypeScript
- Python
- Rust
- Go
OutputStream that the handler has not used also travels as its ref. When the handler has
started to read the stream, or has built the stream itself, the SDK creates a new pipe, and the data
goes through the forwarding worker.
A forwarded pipe keeps its owner. See
Pipe ownership.
Abort a stream
Abort a stream when the handler cannot send all of its data. The reader then receives the data sent before the abort, and then an error with the codeSTREAM_ABORTED. The error shows that the data is
incomplete.
A stream aborts in these cases:
- The handler calls
abort(reason)on anOutputStream. If the socket of the stream is not open,abortopens it first, so the reader always gets the error. - The handler throws after it used an
OutputStreamthat it received. The SDK aborts the stream. - A source that the handler gave to the SDK throws while the SDK reads it, for example a generator that fails. The SDK aborts the stream.
- The worker crashes or loses its connection. The engine aborts the stream.
reason. The engine sends its own text in place of it: see
Close and abort. Only a call that the SDK runs in
the same process keeps the reason. To tell the caller the cause, fail the call or put the cause in
the result. The result reaches a caller that reads the stream through a pipe. A caller that passed
a sink does not get it: the abort fails that call with STREAM_ABORTED, also when the function
returns a result. See Sink errors.
- Node / TypeScript
- Python
- Rust
- Go
Manage the Stream lifecycle
When the handler returns or throws, the SDK ends the streams that the handler received. The table shows what the SDK does with each stream. Most handlers need no cleanup code.
A handler uses an
OutputStream when it writes to it, sends a message on it, or takes its native
API with toWritable() or toWebStream(). A handler uses an InputStream when it starts a read.
A cancelled InputStream cannot be read, and the engine closes the writer of the stream. A read
that still runs after the handler returns, for example in a task that the handler started, fails
with STREAM_CLOSED. An InputStream has no keepOpen(), so finish each read before the handler
returns.
The same rules apply when the SDK runs a call to a function of the same worker in the process,
without a pipe.
Calls through an observer
One rule is different when a worker calls the handler through an observer. The SDK closes anOutputStream that the handler did not use only when the handler returns no value (null or
undefined). When the handler returns a value, the SDK does not change the stream.
The worker that makes the call sets what it means when the handler returns a value and does not use
the stream. For example, the HTTP worker calls a route through an observer. When the route returns a
value and does not use response, the HTTP worker sends the value as a buffered response.
If the caller passed its own sink, a handler that returns a value must use or close the sink. If the
handler does not, the sink stays open, and the caller’s call fails with TIMEOUT when its timeout
expires.
Keep writing after the handler returns
A handler can return its result first and write to anOutputStream later. Call keepOpen() on the
stream before the handler returns. The SDK then does not close or abort the stream when the handler
returns or throws. Your code must close or abort the stream when the work ends.
- Node / TypeScript
- Python
- Rust
- Go
keepOpen() changes only what the SDK does with the stream when the handler returns. It does not
change when the caller’s call resolves: when the caller passed a sink, the call resolves only after
the stream ends.
Payload size
A call without a stream sends its payload in one JSON message and receives its result in another. Each JSON message has a size limit. The limit applies to the envelope: the complete JSON message, with the payload or the result inside it. The size is the number of bytes of the serialized UTF-8 JSON. To send more data, use a stream. Only the ref of the stream counts toward the limit. See Send a stream in a call.Limits
A large message crosses the connection in WebSocket frames:
- The Rust, Node, Python, and Go SDKs send a message of 16 MiB or less as one text frame. They send a larger message, up to and including 64 MiB, as one fragmented WebSocket text message.
- The browser SDK does not fragment messages, because the browser WebSocket API gives no control of frames.
- The engine sends each message larger than 1 MiB to a worker as one fragmented text message with frames of 1 MiB, so that no frame is larger than the frame size limit of a worker.
- The engine accepts a message as one frame or as fragments.
Receive limit of a worker
Before the engine sends a message to a worker, it compares the size of the message with the receive limit of that worker. If the message is larger, the engine does not send it, and the caller gets the errorpayload_too_large.
The engine sets the receive limit when the worker registers:
- A browser worker gets 16 MiB.
- A worker that declares the
large-messages.v1protocol feature gets 64 MiB. The version that the worker reports has no effect. The Rust, Node, Python, and Go SDKs declare the feature. The browser SDK does not declare it. - Each other worker gets 64 MiB or 16 MiB. The engine sets this limit from the SDK version
that the worker reports. A worker that reports no version, or a version that the engine cannot
read, gets 16 MiB. Other features, such as
observe.v1, do not change the limit.
Messages over the limit
- The Rust, Node, Python, and Go SDKs do not send a message over 64 MiB. The SDK refuses the
message with the error
payload_too_large, and the error message links to this section. The browser SDK does the same over 16 MiB. - The engine drops a message over 64 MiB that a worker sends. The connection stays open, and the
other calls on the connection continue. For a call, the worker that sent the call gets
payload_too_largeas the result. For a result, the caller that waits for the result getspayload_too_large. The engine cannot answer other messages, such as a call that expects no result, so it logs them.
input and output both count. The engine does not deliver an
event that is over a limit: the observer counts a TOO_LARGE drop, and the engine logs a warning.
See Observers / Observed call size.
Go and the browser SDK
The Go SDK and the browser SDK use the same stream model as the TypeScript SDK. Their APIs differ in these points. In Go:- Each read method takes a
context.Context.Readis the exception: it takes no context, so anInputStreamis anio.Reader. Lines,Messages, andFramesreturn iterators. An early exit from the loop cancels the stream.- After a read fails, each later read returns the same error.
- The package
github.com/iii-hq/iii/sdk/packages/go/iii/helpers/httpwraps the streams of an HTTP route.iiihttp.Wrap(fn)callsfnwith aStreamRequest, which is anio.Readeroverrequest_body, and aStreamResponsewithStatus,Headers, andWrite. The SDK closes or aborts the response by the rules in Manage the Stream lifecycle.
- The API is the TypeScript API with Web streams only. A source is a
ReadableStream, aBlob, aFile, an async generator, or anInputStream. A sink is aWritableStreamor anOutputStream. The only native API istoWebStream(). - The ref helpers are in
iii-browser-sdk/protocol. - An abort closes the socket of the stream with code 4011, because a browser can send only code 1000
or a code from 3000 to 4999. The engine treats 4011 as an abort. The
reasonthat the reader gets ischannel writer aborted: closed with code 4011. writeresolves when the socket holds 1 MiB or less of unsent data.close()waits until the socket has sent all the data.- Reads have no backpressure. A page cannot pause its socket, so the frames that arrive before the page reads them stay in memory. Read a stream as it arrives, or cancel it.
- The URL of a stream’s socket keeps the query string of the engine URL, such as
?token=.