From 5acaaace7fefd0f587ef7a2857492b68b01c2a2c Mon Sep 17 00:00:00 2001 From: katereznykova Date: Mon, 15 Dec 2025 10:07:44 +0000 Subject: [PATCH 1/3] update ws example --- .../sandbox/guides/websocket-connections.mdx | 49 +++++++++++++------ 1 file changed, 35 insertions(+), 14 deletions(-) diff --git a/src/content/docs/sandbox/guides/websocket-connections.mdx b/src/content/docs/sandbox/guides/websocket-connections.mdx index 090152e2e33..8b3d984d98c 100644 --- a/src/content/docs/sandbox/guides/websocket-connections.mdx +++ b/src/content/docs/sandbox/guides/websocket-connections.mdx @@ -108,7 +108,7 @@ Get a public URL for your WebSocket server: ```ts import { getSandbox, proxyToSandbox } from '@cloudflare/sandbox'; -export { Sandbox } from '@cloudflare/sandbox'; +export { Sandbox } from "@cloudflare/sandbox"; export default { async fetch(request: Request, env: Env): Promise { @@ -129,6 +129,7 @@ export default { } return new Response('Not found', { status: 404 }); + } }; @@ -156,25 +157,45 @@ Your Worker can connect to a WebSocket service to get real-time data, even when ```ts import { getSandbox } from '@cloudflare/sandbox'; -export { Sandbox } from '@cloudflare/sandbox'; +export { Sandbox } from "@cloudflare/sandbox"; + +const initialized = new Set(); export default { async fetch(request: Request, env: Env): Promise { - const sandbox = getSandbox(env.Sandbox, 'data-processor'); - // Incoming HTTP request needs real-time data from sandbox - const wsRequest = new Request('ws://internal', { - headers: { - 'Upgrade': 'websocket', - 'Connection': 'Upgrade' - } - }); + // Get or create a sandbox instance + const sandbox = getSandbox(env.Sandbox, 'data-processor'); - // Connect to WebSocket service in sandbox - const wsResponse = await sandbox.wsConnect(wsRequest, 8080); - // Process WebSocket stream and return HTTP response - // (Implementation depends on your needs) + // Check for WebSocket upgrade + const upgrade = request.headers.get('Upgrade')?.toLowerCase(); + + if (upgrade === 'websocket') { + // Initialize server on first connection + if (!initialized.has('websocket-app')) { + await sandbox.writeFile( + '/workspace/server.js', + `Bun.serve({ + port: 8080, + fetch(req, server) { + server.upgrade(req); + }, + websocket: { + message(ws, msg) { + ws.send(\`Echo: \${msg}\`); + } + } + });` + ); + await sandbox.startProcess( + 'bun /workspace/server.js' + ); + initialized.add('websocket-app'); + } + // Connect to WebSocket server + return await sandbox.wsConnect(request, 8080); + } return new Response('Processed real-time data'); From 6b9643ff762b12ca61379003c03369b29b15ed0e Mon Sep 17 00:00:00 2001 From: whoiskatrin Date: Mon, 15 Dec 2025 10:12:31 +0000 Subject: [PATCH 2/3] Fix export statement quotes in websocket connections guide --- src/content/docs/sandbox/guides/websocket-connections.mdx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/content/docs/sandbox/guides/websocket-connections.mdx b/src/content/docs/sandbox/guides/websocket-connections.mdx index 8b3d984d98c..483109ec073 100644 --- a/src/content/docs/sandbox/guides/websocket-connections.mdx +++ b/src/content/docs/sandbox/guides/websocket-connections.mdx @@ -108,7 +108,7 @@ Get a public URL for your WebSocket server: ```ts import { getSandbox, proxyToSandbox } from '@cloudflare/sandbox'; -export { Sandbox } from "@cloudflare/sandbox"; +export { Sandbox } from '@cloudflare/sandbox'; export default { async fetch(request: Request, env: Env): Promise { @@ -157,7 +157,7 @@ Your Worker can connect to a WebSocket service to get real-time data, even when ```ts import { getSandbox } from '@cloudflare/sandbox'; -export { Sandbox } from "@cloudflare/sandbox"; +export { Sandbox } from '@cloudflare/sandbox'; const initialized = new Set(); From d4366d81292f7b0d2a0de6aea5aea6859b61c23c Mon Sep 17 00:00:00 2001 From: whoiskatrin Date: Mon, 15 Dec 2025 10:21:48 +0000 Subject: [PATCH 3/3] Refactor WebSocket initialization logic --- src/content/docs/sandbox/guides/websocket-connections.mdx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/content/docs/sandbox/guides/websocket-connections.mdx b/src/content/docs/sandbox/guides/websocket-connections.mdx index 483109ec073..2a9eaa2c271 100644 --- a/src/content/docs/sandbox/guides/websocket-connections.mdx +++ b/src/content/docs/sandbox/guides/websocket-connections.mdx @@ -159,7 +159,7 @@ import { getSandbox } from '@cloudflare/sandbox'; export { Sandbox } from '@cloudflare/sandbox'; -const initialized = new Set(); +let initialized = false; export default { async fetch(request: Request, env: Env): Promise { @@ -173,7 +173,7 @@ export default { if (upgrade === 'websocket') { // Initialize server on first connection - if (!initialized.has('websocket-app')) { + if (!initialized) { await sandbox.writeFile( '/workspace/server.js', `Bun.serve({ @@ -191,7 +191,7 @@ export default { await sandbox.startProcess( 'bun /workspace/server.js' ); - initialized.add('websocket-app'); + initialized = true; } // Connect to WebSocket server return await sandbox.wsConnect(request, 8080);