Skip to content

WebSockets

Standard WebSocket upgrade, plus hibernation on Durable Objects.

ts
export default {
  async fetch(request: Request): Promise<Response> {
    if (request.headers.get("Upgrade") !== "websocket") {
      return new Response("expected websocket", { status: 426 });
    }
    const pair = new WebSocketPair();
    pair[1].accept();
    pair[1].send("hello");
    return new Response(null, { status: 101, webSocket: pair[0] });
  },
} satisfies ExportedHandler;

DO hibernation: state.acceptWebSocket, tags, webSocketMessage / webSocketClose / webSocketError, serialize/deserialize attachment. See WebSockets and the Durable Objects hibernation API.

Compatibility

TopicCloudflareopen-compute
WebSocketPair, accept(), messages and closeYesYes
Hibernation: accept / tags / get, auto-response, attachment reconstructionYesYes
Where connections landGlobal-edge upgradeOne workerd on this node
Duration billingYesNot provided
Durable Object placementGlobal placementThis node