Skip to content

feat(cloudflare-workers): add WebSocket Hibernation API support#4879

Open
PAMulligan wants to merge 2 commits intohonojs:mainfrom
PMDevSolutions:feature/websocket-hibernation-api
Open

feat(cloudflare-workers): add WebSocket Hibernation API support#4879
PAMulligan wants to merge 2 commits intohonojs:mainfrom
PMDevSolutions:feature/websocket-hibernation-api

Conversation

@PAMulligan
Copy link
Copy Markdown

@PAMulligan PAMulligan commented Apr 13, 2026

Summary

This PR adds support for Cloudflare's WebSocket Hibernation API in Durable Objects, addressing #4506.

New exports from hono/cloudflare-workers:

  • createWSContext(ws) - Wraps a raw Cloudflare WebSocket into Hono's WSContext for use in Hibernation API handlers (webSocketMessage, webSocketClose, webSocketError)

  • upgradeWebSocketForDO(ctx, options?) - Handles WebSocket upgrade using ctx.acceptWebSocket() instead of server.accept(), with optional tag support for getWebSockets(tag)

Why this matters

The Hibernation API enables long-lived WebSocket connections in Durable Objects without incurring billing charges when connections are dormant; significant cost savings for apps with idle connections.

Example usage

import { Hono } from 'hono'
import { createWSContext, upgradeWebSocketForDO } from 'hono/cloudflare-workers'

export class ChatRoom extends DurableObject {
  app = new Hono()

  constructor(ctx: DurableObjectState, env: Env) {
    super(ctx, env)
    this.app.get('/ws', (c) => upgradeWebSocketForDO(this.ctx, { tags: ['chat'] }))
  }

  fetch(request: Request) {
    return this.app.fetch(request)
  }

  webSocketMessage(ws: WebSocket, message: string | ArrayBuffer) {
    const wsCtx = createWSContext(ws)
    wsCtx.send(`Echo: ${message}`)
  }
}

Test plan

  • Added unit tests for createWSContext (4 test cases)
  • Added unit tests for upgradeWebSocketForDO (5 test cases)
  • All existing WebSocket tests pass
  • Manually tested with a Durable Object demo app

Closes #4506

Add helpers for Cloudflare Durable Objects using the Hibernation API:

- `createWSContext(ws)` - Wraps raw WebSocket into Hono's WSContext
  for use in Hibernation API handlers (webSocketMessage, webSocketClose, etc.)

- `upgradeWebSocketForDO(ctx, options?)` - Handles WebSocket upgrade
  using ctx.acceptWebSocket() instead of server.accept(), with optional
  tag support for getWebSockets(tag)

This enables cost-effective long-lived WebSocket connections that don't
incur billing charges when dormant.

Closes honojs#4506
@codecov
Copy link
Copy Markdown

codecov bot commented Apr 15, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.91%. Comparing base (1aa32fb) to head (b0e8d27).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4879      +/-   ##
==========================================
+ Coverage   92.89%   92.91%   +0.01%     
==========================================
  Files         177      177              
  Lines       11797    11824      +27     
  Branches     3515     3523       +8     
==========================================
+ Hits        10959    10986      +27     
  Misses        837      837              
  Partials        1        1              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

The MockResponse class declares webSocket as a property and the
constructor parameter types it explicitly, so the assignment is
valid TypeScript — no error to suppress.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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.

Add support for Cloudflare's Websocket Hibernation API

1 participant