Skip to content

Add a websocket example#431

Merged
ghostwriternr merged 2 commits intomainfrom
example-websocket-tunnel
Mar 24, 2026
Merged

Add a websocket example#431
ghostwriternr merged 2 commits intomainfrom
example-websocket-tunnel

Conversation

@aron-cf
Copy link
Copy Markdown
Contributor

@aron-cf aron-cf commented Feb 26, 2026

This example sets up a Bun webserver with a simple WebSocket client
plus a basic browser client for connecting to the server via a worker
and sending ping/pong messages back and forth.

This demonstrates how to start a webserver and proxy the requests
through to the sandbox.

The browser client is a single HTML page served via an assets binding
to serve the HTML file.


Open with Devin

@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Feb 26, 2026

⚠️ No Changeset found

Latest commit: d240df7

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@aron-cf aron-cf marked this pull request as draft February 26, 2026 18:06
Copy link
Copy Markdown
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OpenCode Review

This is a well-structured WebSocket tunneling example that demonstrates key sandbox features. The implementation follows repository patterns and serves as useful documentation.

What it accomplishes:

  • Shows how to expose WebSocket services via exposePort() and proxyToSandbox()
  • Demonstrates the preview URL pattern for connecting to sandbox services
  • Provides a working ping/pong WebSocket implementation using Bun

Minor issues to address:

Duplicate HTML files: Both public/index.html and public/template.html are identical. Only one is needed - the template.html appears unused and should be removed.

Port exposure timing: The code calls exposePort() before starting the server process. While this works, it's more conventional to start the process first, then expose the port. Consider reordering for clarity:

await sandbox.startProcess('bun /app/server.js', { processId: 'ws-server' });
await sandbox.waitForPort({ portToCheck: WS_PORT });
const p = await sandbox.exposePort(WS_PORT, { hostname: url.host });

Documentation consistency: The README mentions both "buttons" (connect/disconnect and send ping) but only describes two. Consider clarifying that there are two buttons total.

The core implementation is solid and follows the documented patterns correctly. Nice work demonstrating the sandbox WebSocket capabilities!

@@ -0,0 +1,208 @@
<!doctype html>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is identical to index.html. Since it's unused, it should be removed.

.getExposedPorts(url.host)
.then((ports) => ports.find((p) => p.port === WS_PORT));
if (!port) {
const p = await sandbox.exposePort(WS_PORT, { hostname: url.host });
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider moving exposePort() after startProcess() and waitForPort() for clearer flow - expose the port only after the service is ready.

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Feb 26, 2026

Open in StackBlitz

npm i https://pkg.pr.new/cloudflare/sandbox-sdk/@cloudflare/sandbox@431

commit: d685081

@github-actions
Copy link
Copy Markdown
Contributor

🐳 Docker Images Published

Default:

FROM cloudflare/sandbox:0.0.0-pr-431-0748868

With Python:

FROM cloudflare/sandbox:0.0.0-pr-431-0748868-python

With OpenCode:

FROM cloudflare/sandbox:0.0.0-pr-431-0748868-opencode

Version: 0.0.0-pr-431-0748868

Use the -python variant if you need Python code execution, or -opencode for the variant with OpenCode AI coding agent pre-installed.


📦 Standalone Binary

For arbitrary Dockerfiles:

COPY --from=cloudflare/sandbox:0.0.0-pr-431-0748868 /container-server/sandbox /sandbox
ENTRYPOINT ["/sandbox"]

Download via GitHub CLI:

gh run download 22454841225 -n sandbox-binary

Extract from Docker:

docker run --rm cloudflare/sandbox:0.0.0-pr-431-0748868 cat /container-server/sandbox > sandbox && chmod +x sandbox

@aron-cf aron-cf force-pushed the example-websocket-tunnel branch from d685081 to 5749cf7 Compare March 24, 2026 15:22
aron-cf and others added 2 commits March 24, 2026 22:18
This example sets up a Bun webserver with a simple WebSocket client
plus a basic browser client for connecting to the server via a worker
and sending ping/pong messages back and forth.

This demonstrates how to start a webserver and proxy the requests
through to the sandbox.

The browser client is a single HTML page served via an assets binding
to serve the HTML file.
@ghostwriternr ghostwriternr force-pushed the example-websocket-tunnel branch from 5749cf7 to d240df7 Compare March 24, 2026 22:19
@ghostwriternr ghostwriternr marked this pull request as ready for review March 24, 2026 22:19
@ghostwriternr ghostwriternr requested a review from scuffi as a code owner March 24, 2026 22:19
@ghostwriternr ghostwriternr enabled auto-merge (squash) March 24, 2026 22:20
Copy link
Copy Markdown
Contributor

@devin-ai-integration devin-ai-integration bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 3 additional findings.

Open in Devin Review

@ghostwriternr ghostwriternr merged commit e769ce4 into main Mar 24, 2026
20 checks passed
@ghostwriternr ghostwriternr deleted the example-websocket-tunnel branch March 24, 2026 22:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants