Skip to main content

Stream output

To stream command output as it is being executed, pass the onStdout, onStderr callbacks to the commands.run() method in JavaScript or the on_stdout, on_stderr callbacks to the commands.run() method in Python.
The SDK asks the sandbox for a keepalive ping every 50 seconds on every stream, so a stream normally stays open for more than an hour even while nothing is written. No single connection is guaranteed that long, so build your client to reconnect. See Stream connections.

Stream input

To send input to a running command’s stdin as you produce it, open an input stream with commands.streamInput() in JavaScript or commands.stream_input() in Python. All chunks go over one connection in the order you send them, so sending many small chunks (for example, keystrokes) is faster than calling commands.sendStdin() once per chunk. Start the command in the background with stdin enabled, then send input to it by its pid:
  • JavaScript and async Python return a handle. send() accepts a string or bytes and queues it in order. close() (or leaving the async with block) ends the stream and waits until the sandbox has received all of the input. It throws if the stream failed, for example when the command doesn’t exist or the sandbox was killed.
  • Sync Python takes any iterable of strings or bytes, such as a list or a generator, and blocks until the iterable is exhausted. Each item is sent as soon as the iterable yields it.
  • Timeout. The input stream closes after 60 seconds by default. Pass timeoutMs: 0 in JavaScript or timeout=0 in Python to keep it open for as long as the command needs input. While a JavaScript or async Python stream is idle, the SDK sends a keepalive every 50 seconds. Sync Python sends no keepalives while it waits for the next item.
In browsers, input streaming needs fetch support for streaming request bodies, which only Chromium-based browsers have, and only over HTTP/2. In other browsers, use commands.sendStdin().