-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixup! Move reference documentation out of Durable Objects best pract…
…ices
- Loading branch information
1 parent
4c2f919
commit 0597b72
Showing
6 changed files
with
15 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,6 @@ pcx_content_type: concept | |
title: Durable Objects with WebSockets | ||
sidebar: | ||
order: 3 | ||
|
||
--- | ||
|
||
[WebSockets](/durable-objects/api/websockets/) allow real time communication between a client and server. Both Cloudflare Durable Objects and Workers can act as WebSocket endpoints – either as a client or as a server. | ||
|
@@ -14,16 +13,14 @@ Durable Objects provide a single-point-of-coordination for [Cloudflare Workers]( | |
|
||
While there are other use cases for using Workers exclusively with WebSockets, WebSockets are most useful when combined with Durable Objects. | ||
|
||
When a client connects to your application using a WebSocket, you need a way for server-generated messages to be sent using the existing socket connection. Multiple clients can establish a WebSocket connection with a specific Durable Object addressed by its [unique ID](/durable-objects/best-practices/access-durable-objects-from-a-worker/#1-create-durable-object-ids). The Durable Object can then send messages to each client over the WebSocket connection. | ||
When a client connects to your application using a WebSocket, you need a way for server-generated messages to be sent using the existing socket connection. Multiple clients can establish a WebSocket connection with a specific Durable Object addressed by its unique ID. The Durable Object can then send messages to each client over the WebSocket connection. | ||
|
||
Durable Objects can use the web standard APIs described in [WebSockets API](/durable-objects/api/websockets/). Refer to [Cloudflare Edge Chat Demo](https://github.com/cloudflare/workers-chat-demo) for an example of using Durable Objects with WebSockets. | ||
|
||
:::caution[WebSockets disconnection] | ||
|
||
|
||
Code updates will disconnect all WebSockets. If you deploy a new version of a Worker, every Durable Object is restarted. Any connections to old Durable Objects will be disconnected. | ||
|
||
|
||
::: | ||
|
||
## WebSocket Hibernation | ||
|
@@ -32,19 +29,17 @@ The WebSocket Hibernation API allows a Durable Object that is not currently runn | |
|
||
:::note | ||
|
||
|
||
Hibernation is only supported when a Durable Object acts as a WebSocket server. Outgoing WebSockets cannot be hibernated as of now. | ||
|
||
|
||
::: | ||
|
||
A Durable Object with WebSockets created via the Hibernation API will not incur billable [Duration (GB-s) charges](/durable-objects/platform/pricing/) during periods of inactivity, unlike Durable Objects using the [regular WebSockets API](/workers/runtime-apis/websockets/). | ||
|
||
The WebSocket Hibernation API includes: | ||
|
||
* Cloudflare-specific extensions to the web standard WebSocket API. | ||
* Related methods on the [`state`](/durable-objects/api/websockets/#state-methods) of the Durable Object. | ||
* [Handler methods](/durable-objects/api/websockets/#handler-methods) that a Durable Object can implement for processing WebSocket events. | ||
- Cloudflare-specific extensions to the web standard WebSocket API. | ||
- Related methods on the [`state`](/durable-objects/api/websockets/#state-methods) of the Durable Object. | ||
- [Handler methods](/durable-objects/api/websockets/#handler-methods) that a Durable Object can implement for processing WebSocket events. | ||
|
||
The WebSocket Hibernation API enables you to terminate (not proxy) WebSocket connections within a Durable Object, and push messages to all connected clients based on state stored within the [Storage API](/durable-objects/api/storage-api/), HTTP fetches to external services, and/or data stored in [R2](/r2/) and [Workers KV](/kv/api/). | ||
|
||
|
@@ -54,16 +49,14 @@ If an event occurs for a hibernated Durable Object's corresponding handler metho | |
|
||
:::caution[Support for local development] | ||
|
||
|
||
Prior to `[email protected]` and Miniflare `v3.20231016.0`, WebSockets did not hibernate when using local development environments such as `wrangler dev` or Miniflare. | ||
|
||
If you are using older versions, note that while hibernatable WebSocket events such as [`webSocketMessage()`](/durable-objects/api/websockets/#websocketmessage) will still be delivered, the Durable Object will never be evicted from memory. | ||
|
||
|
||
::: | ||
|
||
*** | ||
--- | ||
|
||
## Related resources | ||
|
||
* Refer to [Build a WebSocket server with WebSocket Hibernation](/durable-objects/examples/websocket-hibernation-server/) to learn more about building a WebSocket server using WebSocket Hibernation on Durable Objects and Workers. | ||
- Refer to [Build a WebSocket server with WebSocket Hibernation](/durable-objects/examples/websocket-hibernation-server/) to learn more about building a WebSocket server using WebSocket Hibernation on Durable Objects and Workers. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters