runStreamingCommand() method is designed for executing long-running or interactive commands inside a sandbox while receiving real-time output streams. Unlike runCommand(), which waits for a command to complete before returning output, this method streams data continuously through callbacks as soon as it’s produced.
It’s the recommended way to handle installations, builds, and background processes such as servers that may run indefinitely. Each chunk of data from stdout or stderr is sent to the provided callbacks immediately, giving you full control over how logs and errors are displayed. Once execution completes, the method returns an object containing the aggregated stdout, stderr, and exitCode.
runStreamingCommand(options)
Description:
Executes a long-running command with real-time output streaming. Use this for installations, builds, servers, or any operation that takes more than 5 seconds. Callbacks are invoked as data arrives.
Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
| cmd | string | Yes | Command to execute |
| args | string[] | Yes | Array of command arguments |
| stdin | string | No | Standard input to send to the command |
| onStdout | (data: string) => void | No | Callback for stdout data chunks |
| onStderr | (data: string) => void | No | Callback for stderr data chunks |
Error: "Not connected"- Must callcreate()orfromSnippet()firstError: "Unexpected response event type"- Internal protocol error
0- Success- Non-zero - Error (e.g.,
1for general errors,127for command not found)
- Callbacks invoked immediately as data arrives
- Data comes in chunks (may be partial lines)
- Use
.trim()to clean up newlines - Both callbacks and return value contain all output
- Always provide
onStdoutandonStderrfor visibility - Check
exitCodeto verify success - For servers, don’t await the promise
- Use stdin for interactive programs
