Skip to content

Commit 65ba003

Browse files
Add WebSocket transport documentation
Documents the new WebSocket transport mode introduced in PR #253. This feature allows multiplexing all SDK requests over a single WebSocket connection, reducing sub-request count in Workers/DOs. Changes: - Add transportMode configuration option in sandbox-options.mdx - Document HTTP vs WebSocket modes with usage examples - Add "When to use transportMode" guidance section - Update architecture diagram to show WebSocket as transport option - Add transport modes explanation in architecture concepts 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 8078de3 commit 65ba003

File tree

2 files changed

+68
-3
lines changed

2 files changed

+68
-3
lines changed

src/content/docs/sandbox/concepts/architecture.mdx

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ flowchart TB
2626
DO["Sandbox Durable Object routes requests & maintains state"]
2727
Container["Isolated Ubuntu container executes untrusted code safely"]
2828
29-
DO -->|HTTP API| Container
29+
DO -->|HTTP API or WebSocket| Container
3030
end
3131
3232
Worker -->|RPC call via the Durable Object stub returned by `getSandbox`| DO
@@ -91,11 +91,27 @@ When you execute a command:
9191
await sandbox.exec("python script.py");
9292
```
9393

94-
1. **Client SDK** validates parameters and sends HTTP request to Durable Object
95-
2. **Durable Object** authenticates and routes to container runtime
94+
1. **Client SDK** validates parameters and sends request to Durable Object
95+
2. **Durable Object** authenticates and routes to container runtime via HTTP or WebSocket
9696
3. **Container Runtime** validates inputs, executes command, captures output
9797
4. **Response flows back** through all layers with proper error transformation
9898

99+
## Transport modes
100+
101+
The SDK supports two transport modes for communication between the Durable Object and container:
102+
103+
**HTTP mode** (default):
104+
- Each SDK method call creates a separate HTTP request
105+
- Simple, reliable, and works everywhere
106+
- Counts each operation toward Worker sub-request limits
107+
108+
**WebSocket mode**:
109+
- All SDK calls multiplex over a single persistent WebSocket connection
110+
- Reduces sub-request count when making many operations
111+
- Useful when approaching Worker sub-request limits (1000 per request in free tier)
112+
113+
Configure the transport mode using the `transportMode` option when creating a sandbox. See [Sandbox options](/sandbox/configuration/sandbox-options/) for details.
114+
99115
## Related resources
100116

101117
- [Sandbox lifecycle](/sandbox/concepts/sandboxes/) - How sandboxes are created and managed

src/content/docs/sandbox/configuration/sandbox-options.mdx

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,44 @@ const sandbox2 = getSandbox(env.Sandbox, 'user-env', {
108108

109109
Precedence: `options` > `env vars` > SDK defaults
110110

111+
### transportMode
112+
113+
**Type**: `"http" | "websocket"`
114+
**Default**: `"http"`
115+
116+
Choose the transport protocol for communication between the Durable Object and container. WebSocket mode multiplexes all requests over a single connection, reducing sub-request count.
117+
118+
**HTTP mode** (default):
119+
- Each SDK method call creates a separate HTTP request
120+
- Simple and works everywhere
121+
- Counts toward sub-request limits in Workers/Durable Objects
122+
123+
**WebSocket mode**:
124+
- All SDK calls multiplex over one WebSocket connection
125+
- Reduces sub-request count - useful when hitting Worker sub-request limits
126+
- Requires `wsUrl` option to be set
127+
128+
<TypeScriptExample>
129+
```ts
130+
// HTTP mode (default) - each call is a separate HTTP request
131+
const sandbox1 = getSandbox(env.Sandbox, 'user-123');
132+
await sandbox1.exec('echo hello'); // HTTP request
133+
await sandbox1.readFile('/file.txt'); // Another HTTP request
134+
135+
// WebSocket mode - all calls share one connection
136+
const sandbox2 = getSandbox(env.Sandbox, 'user-456', {
137+
transportMode: 'websocket',
138+
wsUrl: 'ws://localhost:3000/ws' // Container WebSocket endpoint
139+
});
140+
await sandbox2.exec('echo hello'); // WS message
141+
await sandbox2.readFile('/file.txt'); // Same WS connection
142+
```
143+
</TypeScriptExample>
144+
145+
:::note[When to use WebSocket mode]
146+
Use WebSocket transport when making many SDK calls in quick succession and approaching Worker sub-request limits. The persistent connection eliminates the overhead of creating new HTTP requests for each operation.
147+
:::
148+
111149
### Logging
112150

113151
**Type**: Environment variables
@@ -198,6 +236,17 @@ Use `keepAlive: true` for:
198236

199237
With `keepAlive`, containers never sleep automatically - use for scenarios where you control the lifecycle explicitly.
200238

239+
## When to use transportMode
240+
241+
Use `transportMode: 'websocket'` when:
242+
243+
- **High-frequency SDK calls** - Making many sandbox operations in quick succession
244+
- **Sub-request limits** - Approaching Worker sub-request limits (1000 per request in free tier)
245+
- **Batch operations** - Performing multiple file operations, commands, or process management tasks
246+
- **Performance optimization** - Reducing latency by reusing a persistent connection
247+
248+
The default HTTP mode works well for most applications. Switch to WebSocket mode if you are hitting sub-request limits or need to optimize for high-frequency operations.
249+
201250
## Related resources
202251

203252
- [Expose services guide](/sandbox/guides/expose-services/) - Using `normalizeId` with preview URLs

0 commit comments

Comments
 (0)