> Canonical URL: https://codingcat.dev/course/cloudflare-agents-durable-workflows-typescript/lesson/cloudflare-agents-sdk-websocket-hibernation

# Real-Time Streaming & WebSocket Hibernation with the Cloudflare Agents SDK | Cloudflare Agents, Durable Objects & Stateful Workflows in TypeScript | CodingCat.dev Course

[🧶 Cloudflare Agents, Durable Objects & Stateful Workflows in TypeScript](/course/cloudflare-agents-durable-workflows-typescript)Section 1.2 · Lesson 2 of 2[← Prev Lesson](/course/cloudflare-agents-durable-workflows-typescript/lesson/durable-objects-actor-model-embedded-sqlite)

# Real-Time Streaming & WebSocket Hibernation with the Cloudflare Agents SDK

![Real-Time Streaming & WebSocket Hibernation with the Cloudflare Agents SDK Architecture Diagram](https://cdn.sanity.io/media-libraries/mlttp0BZbCpV/images/ed71255cb816b3448298ec51808f7a31b0e5abe4-1280x720.jpg?w=1920&h=1080&q=82&fit=max&auto=format)[@CodingCatDev62.5K / 100K Subscribers Goal](https://www.youtube.com/@codingcatdev?sub_confirmation=1)[Subscribe on YouTube→](https://www.youtube.com/@codingcatdev?sub_confirmation=1)[Watch on YouTube App ↗](https://www.youtube.com/watch?v=f6tbO9SUHMU&list=PLR0ZSOt97grA)

## 1. The 15-Second Hook

Keeping ten thousand WebSockets open to an AI agent used to mean paying for idle server memory all day long. Let's see how WebSocket Hibernation and the Cloudflare Agents SDK keep sockets open while billing zero during idle pauses.

## 2. Architecture Mental Model (Visual Diagram)

On the left, traditional WebSocket servers bill for idle wall-clock duration while users read responses. On the right, Cloudflare's edge holds the WebSocket connection while your Agent Durable Object hibernates for free and wakes instantly on message.

## 3. Show Me the Code (src/agents/codingcat-agent.ts)

Copied! 🐾📄 src/agents/codingcat-agent.tstypescript

```
import { Agent } from "agents";

export class CodingCatAgent extends Agent {
  onConnect(connection: any) {
    connection.send(JSON.stringify({ type: "connected", state: this.state }));
  }
  async onMessage(connection: any, message: string) {
    const next = (this.state?.count || 0) + 1;
    this.setState({ count: next });
    connection.send(JSON.stringify({ type: "ack", count: next }));
  }
}
```

In src/agents/codingcat-agent.ts, extending the Agent class gives us onConnect, onMessage, and automatic state persistence and client broadcasting via this.setState.

## 4. Production Trade-Off Matrix

Comparing the Cloudflare Agents SDK with WebSocket Hibernation against always-on socket servers, you eliminate idle duration billing and get built-in SQLite state and alarms.

## 5. Verdict & AJ Pro-Tip

> AJ Pro-Tip: Use this.schedule() inside your Agent class whenever an agent needs to check on a long-running background task or send a follow-up reminder minutes or hours later.

That completes our Cloudflare Agents and Durable Objects deep dive! Hit Subscribe to help CodingCat.dev reach 100K subscribers, and watch for tomorrow's new 2026 course!

✏️ Visual Architecture Diagram

## how this course connects together.

Follow AJ from first principles to production-ready architecture.

Let’s build it!✏️ Visual Diagram: Cloudflare Agents, Durable Objects & Stateful Wo…✓ Good PatternVISUAL DIAGRAM[Diagram Walkthrough]Step-by-step diagram breakdown for Cloudflare Agents, Durable Objects & Stateful Workflows in TypeScript: Real-Time Streaming & WebSocket Hibernation with the Cloudflare Agents SDK: start with Core Architecture & Implementation, implement Cloudflare Agents, Durable Objects & Stateful Workflows in TypeScript, and ship to production.AAA Contrast Verified[Course Outline](/course/cloudflare-agents-durable-workflows-typescript)

## Cloudflare Agents, Durable Objects & Stateful Workflows in TypeScript

2/21

### Core Architecture & Implementation

- [1.1Durable Objects Mental Model: Single-Threaded Actors + Embedded SQLite](/course/cloudflare-agents-durable-workflows-typescript/lesson/durable-objects-actor-model-embedded-sqlite)
- [1.2Real-Time Streaming & WebSocket Hibernation with the Cloudflare Agents SDK▶](/course/cloudflare-agents-durable-workflows-typescript/lesson/cloudflare-agents-sdk-websocket-hibernation)

@CodingCatDev on YouTube62.5K / 100K

Subscribe for new course lessons and architecture deep dives every week.

[Subscribe for New Lessons →](https://www.youtube.com/@codingcatdev?sub_confirmation=1)