Skip to main content
A stream is a value in a call’s payload or result. Use a stream for data that is larger than the JSON message limit, or for data that the sender makes over time. For small JSON values, use a regular worker.call(...). The function on the other side receives an InputStream to read or an OutputStream to write. The two functions can run in different processes and languages.
The examples use the stream API of the 0.27 SDKs. To write the functions that receive and return streams, see Creating workers / Streaming. To move code from the channel API, see Upgrading from 0.26.x. For how a stream works, see Understanding iii / Streaming.

Send a stream in a call

To send a stream, put it in the payload of the call. For the steps that the SDK and the engine do, see How a stream works. Each SDK sends only specific types of values as streams. The supported types are listed in the comments on each language tab. Strings, buffers, plain objects, and arrays will not be sent as streams by default. To send these types as streams first convert them with InputStream.from().
A payload can contain more than one stream. The SDK finds streams in plain objects and arrays, at any depth. It creates one pipe for each stream, all at the same time, before it sends the call. If the SDK cannot create a pipe, the call fails and the SDK does not send it. Your worker owns the pipe of each stream that it sends. Keep the worker connected until the transfer has completed. For what the engine does when the owner disconnects, see Pipe ownership. The SDK writes the data of a source into the pipe for you. You write the data yourself when you have an OutputStream: the output of worker.pipe(), or an OutputStream in the result of a call. These are the operations of an OutputStream:
The operation tables on this page show the TypeScript API. The SDK reference gives the same operations for each SDK: Node, Python, Rust, Go, and Browser.

Read a stream from a result

A function can return a stream in its result. The result of worker.call(...) then contains an InputStream in the field where the function put the stream.
An InputStream from a result and the input of a pipe have the same read operations. You can read an InputStream only once, so use the one operation that fits the data: A stream can carry bytes and JSON messages. The operations that read bytes skip the messages, and messages() skips the bytes. To read both types in the order in which the writer sent them, use frames(). A frame is { type: "binary", value } for bytes, or { type: "text", value } for one message as a string. messages() parses each message as JSON. If a message is not valid JSON, messages() gives the message as a string. For the two types of data, see Bytes and messages.

Read a stream more than once

A second read of the same InputStream fails with STREAM_IN_USE. To use the data in two places, call tee() before the first read. tee() returns two copies of the stream, and each copy gives all the data. The two copies are in the same process.
Read the two copies at the same time. tee() keeps at most 16 frames for the copy that is behind. When one copy is 16 frames behind the other, the read of the faster copy waits. If you read one copy to the end before you start the other, the first read waits after 16 frames and does not finish. If you do not need one of the copies, call cancel() on it. The read of the other copy then does not wait. To send the data in another call, put one of the copies in the payload. The SDK refuses the stream that you split with tee(), and the call fails with STREAM_IN_USE.

Receive data into a sink

A sink is a destination that receives the data of a stream, for example a file that is open for writing. Put the sink in the payload. The function receives it as an OutputStream and writes to it, and the SDK copies the bytes into your destination. The call resolves after the sink has received all the data, so the data is complete when the call returns.
A sink receives bytes only. If the function sends messages, the sink drops them. To receive messages, give the function the output of a pipe. See Create a pipe.

Sink errors

A call with a sink can fail in three ways:
  • The stream aborts. The call fails immediately with STREAM_ABORTED. The call fails also when the function returned a result.
  • The sink fails. For example, the disk is full. The SDK stops the copy, and the call fails immediately with STREAM_ABORTED. The reason of the error starts with the sink failed:, and the cause of the error is the error from the sink.
  • The call fails. The SDK stops the copy and closes the sink with the error.
When a stream aborts or a sink fails, the SDK also aborts the other open sinks of the call. After each of these failures, the sink may contain a partial copy of the data.

Sink timeout

The timeoutMs of a call includes the time that its sinks take to receive the data. The time starts when the SDK sends the call. If the time expires before all the sinks are complete, the SDK aborts the sinks that are still open, and the call fails with TIMEOUT. Set a longer timeoutMs when a sink fills slowly. For example, some functions continue to write after they return. A call does not wait for its sinks when its result contains an OutputStream for you to write to. The data that the sinks receive can depend on the data that you write, so the call resolves when the function returns. A call with CallAction.Void does not wait for its sinks either. In these two cases, a sink that aborts or fails after the call resolved cannot fail the call. A sink that has already failed when the function returns still fails a call whose result contains an OutputStream.

Create a pipe

When you put a stream or a sink in a payload, the SDK creates the pipe for you. worker.pipe() is how you can create a pipe manually. It returns the two ends of the pipe: you write to output, and you read the same data from input. Put one end in a payload, and use the other end in your code. Use worker.pipe() in these cases:
  • The function must receive a stream before you have its data.
  • You make the call that uses the stream at a later time.
  • You need the messages that the function sends. A sink keeps only the bytes and drops the messages.
  • You need a two-way exchange. Use two pipes, one for each direction.
The engine can hold a number of frames between the writer and the reader of a pipe. The default is 64 frames. To change the number, call worker.pipe({ bufferSize }). The maximum is 1024 frames. Your worker owns each pipe that it creates with pipe(). See Pipe ownership. A function can fail before it writes to the output that you gave it. The output then stays open, and your read of input waits until the engine drops the output connection. The error of the call arrives first, so monitor the call while you read. When the call fails, you can stop the read with input.cancel(). The read then fails with STREAM_CLOSED.
A pipe end is different from a sink. A call with a sink resolves after the stream ends. A call with a pipe end resolves when the function returns.

Handle aborts and errors

A read of a stream ends in one of two ways. If the read ends without an error, the stream closed cleanly and you have all the data. If the stream aborts, the read gives you the data that the writer sent before the abort, and then throws a StreamAbortedError with the code STREAM_ABORTED. When you get this error, the data is incomplete. Do not use it as the full stream.
The reason of the error does not contain the root cause of the failure. The reason comes from the engine, for example channel writer aborted: closed with code 1011. To get the cause, read the error or the result of the call. For how the engine relays an abort, see Close and abort.

Error codes

The SDK reports each stream error as an InvocationError with one of these codes:
  • STREAM_ABORTED: The stream aborted before its end.
  • STREAM_CLOSED: A write or a send came after the stream closed, aborted, or lost its reader. A read also fails with this code when the InputStream was cancelled. A stream is cancelled by cancel(), by an early exit from a read loop, or when the handler that received it returns.
  • STREAM_IN_USE: An InputStream was read a second time, the same stream is in one payload two times, or the stream was already read to the end or already sent.
  • STREAM_NOT_ALLOWED: A queued call has a stream in its payload. See Queue a call.

Queue a call

A queued call cannot carry a stream. If the payload of a call with CallAction.Enqueue contains a stream, the SDK does not send the call, and the call fails with STREAM_NOT_ALLOWED. There are two reasons:
  • The queue can run the call after the pipe of the stream expired.
  • When the queue runs a failed call again, the function cannot read the stream a second time.
To queue work on such data, store the data first and put its key in the payload. For example, upload a file to object storage and queue the key of the object.
CallAction.Void is different from CallAction.Enqueue. A call with CallAction.Void can carry a stream. The function runs immediately, and the SDK does not wait for the result or for the sinks of the call.

Observe a stream

You can observe a function that receives or returns streams. The target of the observer does not receive the streams. In the input or output that the observer captures, the target receives a StreamInfo in place of each stream: { stream: true, direction, id }. A StreamInfo shows that the call carried a stream, with the direction of the stream and the id of its pipe. It has no access key, so the target cannot read or write the stream. The other fields arrive as usual, and filters operate as usual. For the reason that the target receives a StreamInfo, see Streams and observers.
The engine sends the ref of the stream, with its access key, and the SDK replaces it. A target on an SDK before 0.27 receives the ref. Do not open it. A stream has one reader, and the first socket that attaches takes the stream from the observed function.