WebSockets
WebSocket procedures enable real-time two-way communication between client and server with zero infrastructure management 🥳.
Important: Shapeless WebSockets are built for Cloudflare Workers only, because Workers support long-lived real-time connections. Node.js platforms like Vercel do not support this.
A WebSocket handler receives:
c
: Hono context (headers, request info, env vars)ctx
: Your app context (DB, auth info, etc.)io
: Connection manager for broadcasting messages
WebSockets Example
WebSockets are great for things like:
- Collaborative editing
- Real-time chat
- Live dashboards
Here’s a simple chat example that validates messages and broadcasts them to all clients in a room:
On the client, you can listen for and send real-time events like this:
Setup
Development
Shapeless uses Upstash Redis as the real-time backend for WebSockets, allowing production-ready WebSockets without any billing.
-
Log in to Upstash and create a Redis database.
-
Add the following env vars to
.dev.vars
:
- Run your opennext locally with:
- Point your client’s
baseUrl
to the Worker backend on port8080
:
Deployment
- Deploy to Cloudflare Workers:
- Set your Redis env vars in Workers:
- Update client to use production URL:
Now your WebSocket client will connect to your Cloudflare Worker backend both locally and in production, with zero infrastructure hassle.