Introducing WebSockets Support in Cloudflare Workers
Apr 14, 2021Kristian Freeman

Summary
Introduced support for WebSockets in Cloudflare Workers, enabling bidirectional communication between clients and Workers. This involves handling the 'Upgrade' header, instantiating WebSocketPair, and using the `webSocket` property on the Response object with a 101 status code. The `handleSession` function demonstrates accepting the WebSocket, listening for 'message' and 'close' events. Client-side connection uses the standard browser WebSocket API. The post highlights the synergy between WebSockets and Durable Objects for building stateful, real-time applications at the edge. It also discusses pricing implications for WebSockets on Workers Unbound and Bundled plans, and proposed pricing for Durable Objects with WebSockets.