Skip to content

Commit ac0a66d

Browse files
joshthowardOxyjun
andauthored
Apply suggestions from code review
Co-authored-by: Jun Lee <[email protected]>
1 parent 63e76c7 commit ac0a66d

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

src/content/docs/durable-objects/best-practices/websockets.mdx

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,25 @@ sidebar:
55
order: 4
66
---
77

8-
import GlossaryTooltip from "~/components/GlossaryTooltip.astro";
9-
import { Tabs, TabItem } from "@astrojs/starlight/components";
8+
import { Tabs, TabItem, GlossaryTooltip } from "~/components";
109

1110
WebSockets are long-lived TCP connections that enable bi-directional, real-time communication between client and server. Both Cloudflare Durable Objects and Workers can act as WebSocket endpoints – either as a client or as a server. Because WebSocket sessions are long-lived, applications commonly use Durable Objects to accept either the client or server connection. While there are other use cases for using Workers exclusively with WebSockets, for example proxying WebSocket messages, WebSockets are most useful when combined with Durable Objects.
1211

1312
Because Durable Objects provide a single-point-of-coordination between [Cloudflare Workers](/workers/), a single Durable Object instance can be used in parallel with WebSockets to coordinate between multiple clients, such as participants in a chat room or a multiplayer game. Refer to [Cloudflare Edge Chat Demo](https://github.com/cloudflare/workers-chat-demo) for an example of using Durable Objects with WebSockets.
1413

15-
Both Cloudflare Durable Objects and Workers can use the [Web Standard WebSocket API](/workers/runtime-apis/websockets/) to build applications, but a major differentiator of Cloudflare Durable Objects relative to other platforms is the ability to Hibernate WebSocket connections to save costs. This guide will first cover building a WebSocket server using web standard APIs and then using WebSocket Hibernation APIs.
14+
Both Cloudflare Durable Objects and Workers can use the [Web Standard WebSocket API](/workers/runtime-apis/websockets/) to build applications, but a major differentiator of Cloudflare Durable Objects relative to other platforms is the ability to Hibernate WebSocket connections to save costs.
15+
16+
This guide covers:
17+
1. Building a WebSocket server using Web Standard APIs
18+
2. Using WebSocket Hibernation APIs.
1619

1720
## WebSocket Standard API
1821

1922
WebSocket connections are established by making an HTTP GET request with the `Upgrade: websocket` header. A Cloudflare Worker is commonly used to validate the request, proxy the request to the Durable Object to accept the server side connection, and return the client side connection in the response.
2023

2124
:::note[Validate requests in a Worker]
2225

23-
Both Workers and Durable Objects are billed in part based on the number of requests they receive. To avoid being billed for requests against a Durable Object for invalid requests, be sure to validate requests in your Worker.
26+
Both Workers and Durable Objects are billed, in part, based on the number of requests they receive. To avoid being billed for requests against a Durable Object for invalid requests, be sure to validate requests in your Worker.
2427

2528
:::
2629

@@ -108,7 +111,7 @@ export default {
108111

109112
</TabItem> </Tabs>
110113

111-
Each WebSocket server in this example is represented by a Durable Object. This WebSocket server creates an single WebSocket connection and responds to all messages over that connection with the total number of accepted WebSocket connections. In the Durable Object's fetch handler we create client and server connections and add event listeners for relevant event types.
114+
Each WebSocket server in this example is represented by a Durable Object. This WebSocket server creates a single WebSocket connection and responds to all messages over that connection with the total number of accepted WebSocket connections. In the Durable Object's fetch handler we create client and server connections and add event listeners for relevant event types.
112115

113116
<Tabs> <TabItem label="JavaScript" icon="seti:javascript">
114117

@@ -214,7 +217,7 @@ export class WebSocketServer extends DurableObject {
214217

215218
</TabItem> </Tabs>
216219

217-
To execute this code configure your `wrangler.toml` file to include a Durable Object [binding](/durable-objects/get-started/#5-configure-durable-object-bindings) and [migration](/durable-objects/reference/durable-objects-migrations/) based on the <GlossaryTooltip term="namespace">namespace</GlossaryTooltip> and class name chosen previously.
220+
To execute this code, configure your `wrangler.toml` file to include a Durable Object [binding](/durable-objects/get-started/#5-configure-durable-object-bindings) and [migration](/durable-objects/reference/durable-objects-migrations/) based on the <GlossaryTooltip term="namespace">namespace</GlossaryTooltip> and class name chosen previously.
218221

219222
```toml title="wrangler.toml"
220223
name = "websocket-server"
@@ -238,17 +241,17 @@ Code updates will disconnect all WebSockets. If you deploy a new version of a Wo
238241

239242
## WebSocket Hibernation API
240243

241-
In addition to [Workers WebSocket API](/workers/runtime-apis/websockets/), Cloudflare Durable Objects can use the WebSocket Hibernation API which extends the web standard WebSocket API to reduce costs. Specifically, [billable Duration (GB-s) charges](/durable-objects/platform/pricing/) are not incurred during periods of inactivity. Note that other events, for example [alarms](/durable-objects/api/alarms/), can prevent a Durable Object from being inactive and therefore prevent this cost savings.
244+
In addition to [Workers WebSocket API](/workers/runtime-apis/websockets/), Cloudflare Durable Objects can use the WebSocket Hibernation API which extends the Web Standard WebSocket API to reduce costs. Specifically, [billable Duration (GB-s) charges](/durable-objects/platform/pricing/) are not incurred during periods of inactivity. Note that other events, for example [alarms](/durable-objects/api/alarms/), can prevent a Durable Object from being inactive and therefore prevent this cost saving.
242245

243-
The WebSocket consists of Cloudflare-specific extensions to the web standard WebSocket API. These extensions are either present on the [DurableObjectState](/durable-objects/api/state) interface or as handler methods on the Durable Object class.
246+
The WebSocket consists of Cloudflare-specific extensions to the Web Standard WebSocket API. These extensions are either present on the [DurableObjectState](/durable-objects/api/state) interface, or as handler methods on the Durable Object class.
244247

245248
:::note
246249

247-
Hibernation is only supported when a Durable Object acts as a WebSocket server. Outgoing WebSockets cannot hibernate currently.
250+
Hibernation is only supported when a Durable Object acts as a WebSocket server. Currently, outgoing WebSockets cannot hibernate.
248251

249252
:::
250253

251-
The Worker used in the WebSocket Standard API example does not require any code changes to make use of the WebSocket Hibernation API. The changes to the Durable Object are described in the code sample below. In summary, [`DurableObjectState::acceptWebSocket`](/durable-objects/api/state/#acceptwebsocket) is called to accept the server side of the WebSocket connection and handler methods are defined on the Durable Object class for relevant event types rather than adding event listeners.
254+
The Worker used in the WebSocket Standard API example does not require any code changes to make use of the WebSocket Hibernation API. The changes to the Durable Object are described in the code sample below. In summary, [`DurableObjectState::acceptWebSocket`](/durable-objects/api/state/#acceptwebsocket) is called to accept the server side of the WebSocket connection, and handler methods are defined on the Durable Object class for relevant event types rather than adding event listeners.
252255

253256
If an event occurs for a hibernated Durable Object's corresponding handler method, it will return to memory. This will call the Durable Object's constructor, so it is best to minimize work in the constructor when using WebSocket hibernation.
254257

@@ -378,5 +381,5 @@ If you are using older versions, note that while hibernatable WebSocket events s
378381

379382
## Related resources
380383

381-
- [Mozilla Developer Network's (MDN) documentation on the WebSocket class](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket).
382-
- [Cloudflare's WebSocket template for building applications on Workers using WebSockets](https://github.com/cloudflare/websocket-template).
384+
- [Mozilla Developer Network's (MDN) documentation on the WebSocket class](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket)
385+
- [Cloudflare's WebSocket template for building applications on Workers using WebSockets](https://github.com/cloudflare/websocket-template)

0 commit comments

Comments
 (0)