Building real-time applications has always been one of the most exciting yet challenging areas of modern web development. Traditionally, WebSockets required dedicated servers, persistent connections, and careful scaling strategies to handle thousands—or even millions—of concurrent users. Thankfully, with the rise of serverless platforms, we can now achieve real-time communication without maintaining traditional server infrastructure.
In this article, we’ll explore how to build serverless WebSockets using Cloudflare Workers, Hono, and Durable Objects. We will walk step by step through the architecture, setup, and implementation with working code examples. By the end, you’ll have a clear roadmap for creating scalable and resilient real-time applications, such as live chat, collaborative editors, multiplayer games, or dashboards.
Understanding The Serverless WebSocket Architecture
Before diving into the implementation, let’s break down the major components we’ll be working with:
-
Cloudflare Workers: A serverless platform that runs lightweight JavaScript code at the edge. Workers handle WebSocket upgrades and routing without servers.
-
Hono Framework: A fast, minimal web framework for Cloudflare Workers (and other runtimes). It helps simplify routing, middleware, and request handling.
-
Durable Objects: A unique feature of Cloudflare Workers that provides strongly consistent state and coordination across multiple connections. This is where WebSocket sessions can be stored, managed, and synchronized.
The key challenge with WebSockets is state management. Since Cloudflare Workers are stateless and ephemeral, they cannot maintain persistent connections alone. This is why Durable Objects are critical—they allow multiple connections to be coordinated across the distributed edge network.
Use Cases For Serverless WebSockets
Before coding, let’s imagine a few real-world use cases:
-
Chat Applications – Users can join a room, send messages, and see real-time updates.
-
Collaboration Tools – Like Google Docs-style editors with live cursors and updates.
-
Gaming – Multiplayer turn-based or real-time games needing synchronization.
-
Real-Time Dashboards – Monitoring stock prices, IoT devices, or logs.
In all these scenarios, state consistency (ensured by Durable Objects) and scalability (handled by Cloudflare Workers) are essential.
Setting Up The Project
Let’s begin by setting up a Cloudflare Worker project using Hono and Durable Objects.
-
Install Wrangler CLI (Cloudflare’s developer tool):
-
Initialize A New Worker Project:
-
Install Hono:
-
Configure wrangler.toml for Durable Objects:
Here, CHAT_ROOM will be our Durable Object responsible for managing WebSocket connections in a given chat room.
Creating The Durable Object
A Durable Object represents a single instance of stateful logic. It can store active WebSocket sessions, broadcast messages, and ensure consistency.
Let’s implement the ChatRoom Durable Object:
This Durable Object maintains:
-
An array of active WebSocket connections.
-
A
broadcast()method to send messages to all connected clients. -
A
fetch()method that upgrades HTTP requests into WebSocket connections.
Connecting Hono With Durable Objects
Now let’s integrate Hono to route requests and connect users to the right Durable Object.
This code allows clients to connect to different chat rooms via /chat/roomName. Each room corresponds to a unique Durable Object instance identified by its name.
Client-Side WebSocket Example
On the frontend, connecting to our WebSocket server is straightforward. Let’s create a simple HTML + JavaScript client:
When multiple users connect to the same room, messages are broadcast to everyone in real time.
Scaling WebSockets Across The Edge
One of the biggest strengths of using Cloudflare Workers with Durable Objects is scalability. Unlike traditional servers, where sticky sessions or load balancers are required, Durable Objects automatically:
-
Ensure a single instance per chat room across the global network.
-
Route all connections for that room to the same Durable Object.
-
Handle reconnections and resilience automatically.
This design is far more efficient and cost-effective than maintaining fleets of WebSocket servers.
Adding Persistence With Durable Objects Storage
Right now, our chat app only broadcasts messages in memory. If the Durable Object restarts, all history is lost. To fix this, we can use this.state.storage for persistence:
This ensures users joining the chat see the latest conversation history.
Enhancing With Hono Middleware
Hono also supports middleware, which means we can add features like authentication or logging before connections are established. For example, requiring a query parameter token:
This small change adds security without complicating the Durable Object logic.
Testing The Setup
You can run the project locally with:
Then open the HTML client, connect to your worker, and open multiple browser tabs. Sending messages in one tab should immediately appear in all others.
When ready, deploy to Cloudflare’s edge:
Benefits Of This Approach
By combining Cloudflare Workers, Hono, and Durable Objects, we achieve:
-
Serverless Scalability – No servers, just code running at the edge.
-
Global Low Latency – Workers run close to users worldwide.
-
Stateful Connections – Durable Objects provide persistent, consistent state.
-
Simple Development – Hono simplifies routing and middleware.
-
Cost Efficiency – Pay only for what you use, without idle servers.
This makes it a great solution for startups, hobby projects, and even enterprise-scale applications.
Conclusion
Building real-time applications used to require heavyweight infrastructure: dedicated servers, scaling strategies, and expensive maintenance. With Cloudflare Workers, Hono, and Durable Objects, that complexity is eliminated.
We’ve seen how Durable Objects solve the challenge of stateful WebSocket management by ensuring one consistent instance per room, enabling features like broadcasting and persistence with ease. Hono provides a lightweight and elegant routing layer, while Workers handle the scaling and performance globally.
The combination of these tools allows developers to create powerful real-time systems without servers—just pure serverless logic running at the edge. Whether you’re building a live chat, multiplayer game, or collaborative editor, this architecture gives you scalability, resilience, and simplicity.
In essence, the future of WebSockets is serverless and global. By adopting Cloudflare’s ecosystem with Hono and Durable Objects, you are embracing a development model that scales seamlessly, reduces complexity, and delivers excellent user experiences worldwide.