Skip to content

Commit 7aacdc2

Browse files
committed
Add WebSocket connection support to sandbox documentation
1 parent 313d562 commit 7aacdc2

File tree

3 files changed

+66
-1
lines changed

3 files changed

+66
-1
lines changed

src/content/docs/sandbox/api/ports.mdx

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,48 @@ for (const port of ports) {
8484
```
8585
</TypeScriptExample>
8686

87+
### `connect()`
88+
89+
Route incoming WebSocket upgrade requests to WebSocket servers running in the sandbox.
90+
91+
```ts
92+
import { connect } from '@cloudflare/sandbox';
93+
94+
const response = await connect(sandbox: Sandbox, request: Request, port: number): Promise<Response>
95+
```
96+
97+
**Parameters**:
98+
- `sandbox` - Sandbox instance containing the WebSocket server
99+
- `request` - Incoming WebSocket upgrade request
100+
- `port` - Port number (1024-65535, excluding 3000)
101+
102+
**Returns**: `Promise<Response>` - WebSocket response establishing the connection
103+
104+
<TypeScriptExample>
105+
```
106+
import { getSandbox, connect } from '@cloudflare/sandbox';
107+
108+
export default {
109+
async fetch(request: Request, env: Env): Promise<Response> {
110+
const sandbox = getSandbox(env.Sandbox, 'my-sandbox');
111+
112+
// Start WebSocket echo server
113+
await sandbox.writeFile('/workspace/server.js', \`
114+
Bun.serve({
115+
port: 8080,
116+
fetch(req, server) { if (server.upgrade(req)) return; },
117+
websocket: { message(ws, msg) { ws.send(\\\`Echo: \${msg}\\\`); } }
118+
});
119+
\`);
120+
await sandbox.startProcess('bun /workspace/server.js');
121+
122+
// Route WebSocket connections
123+
return await connect(sandbox, request, 8080);
124+
}
125+
};
126+
```
127+
</TypeScriptExample>
128+
87129
## Related resources
88130

89131
- [Preview URLs concept](/sandbox/concepts/preview-urls/) - How preview URLs work

src/content/docs/sandbox/concepts/preview-urls.mdx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ const admin = await sandbox.exposePort(3001, { name: "admin" });
7979

8080
- Raw TCP/UDP connections
8181
- Custom protocols (must wrap in HTTP)
82-
- WebSocket connections
8382
- Ports outside range 1024-65535
8483
- Port 3000 (used internally by the SDK)
8584

src/content/docs/sandbox/index.mdx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,30 @@ df['sales'].sum() # Last expression is automatically returned
112112
};
113113
```
114114
</TabItem>
115+
<TabItem label="WebSocket Connections">
116+
```typescript
117+
import { getSandbox, connect } from '@cloudflare/sandbox';
118+
119+
export default {
120+
async fetch(request: Request, env: Env): Promise<Response> {
121+
const sandbox = getSandbox(env.Sandbox, 'user-123');
122+
123+
// Start a WebSocket echo server
124+
await sandbox.writeFile('/workspace/server.js', \`
125+
Bun.serve({
126+
port: 8080,
127+
fetch(req, server) { if (server.upgrade(req)) return; },
128+
websocket: { message(ws, msg) { ws.send(\\\`Echo: \${msg}\\\`); } }
129+
});
130+
\`);
131+
await sandbox.startProcess('bun /workspace/server.js');
132+
133+
// Route WebSocket connections to the server
134+
return await connect(sandbox, request, 8080);
135+
}
136+
};
137+
```
138+
</TabItem>
115139

116140
</Tabs>
117141

0 commit comments

Comments
 (0)