-
Notifications
You must be signed in to change notification settings - Fork 10.3k
[DO] Adding Durable Object base class #18423
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 12 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
46edf37
Adding a new
Oxyjun 06904e0
Adding Durable Object Base Class chapter. Adding cross-references.
Oxyjun d713eff
Adding link to related resources.
Oxyjun e34e07e
Adding extended methods to DO State page.
Oxyjun cac7c34
Merge commit 'a6008b338a24e83636048562e947d97b9c9a777f' into jun/do/b…
Oxyjun 6088c07
Adding fetch and alarm. Actioning PR comments.
Oxyjun 5a9944d
Revert "Adding fetch and alarm. Actioning PR comments."
Oxyjun d9e7768
Adding fetch and alarm to base class chapter.
Oxyjun fadcb3b
Adding alarm handler method into DO base class.
Oxyjun cb2ba3e
Importing missing package.
Oxyjun 1012ba5
Adding missing component x2
Oxyjun 26a7a44
Clarifying language on serializeAttachment method.
Oxyjun 0ada43d
Apply suggestions from code review
Oxyjun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| --- | ||
| title: Durable Object Base Class | ||
| pcx_content_type: concept | ||
| sidebar: | ||
| order: 1 | ||
| --- | ||
|
|
||
| import { Render, Tabs, TabItem, GlossaryTooltip, Type, MetaInfo, TypeScriptExample } from "~/components"; | ||
|
|
||
| The `DurableObject` base class is an abstract class which all Durable Objects inherit from. This base class provides a set of optional methods, frequently referred to as handler methods, which can respond to events, for example a webSocketMessage when using the [WebSocket Hibernation API](#/durable-objects/best-practices/websockets/#websocket-hibernation-api). To provide a concrete example, here is a Durable Object `MyDurableObject` which extends `DurableObject` and implements the fetch handler to return "Hello, World!" to the calling Worker. | ||
|
|
||
| <TypeScriptExample> | ||
| ```ts | ||
| export class MyDurableObject extends DurableObject { | ||
| constructor(ctx: DurableObjectState, env: Env) { | ||
| super(ctx, env); | ||
| } | ||
|
|
||
| async fetch(request: Request) { | ||
| return new Response("Hello, World!"); | ||
| } | ||
| } | ||
| ``` | ||
| </TypeScriptExample> | ||
|
|
||
| ## Methods | ||
|
|
||
| ### `fetch` | ||
|
|
||
| - <code>fetch(<Type text="Request"/>)</code>: <Type text="Response"/> | <Type text="Promise <Response>"/> | ||
|
|
||
| - Takes an HTTP request object and returns an HTTP response object. This method allows the Durable Object to emulate an HTTP server where a Worker with a binding to that object is the client. | ||
|
|
||
| - This method can be `async`. | ||
|
|
||
| ### `alarm` | ||
|
|
||
| - <code>alarm()</code>: <Type text="Promise <void>"/> | ||
|
|
||
| - Called by the system when a scheduled alarm time is reached. | ||
|
|
||
| - The `alarm()` handler has guaranteed at-least-once execution and will be retried upon failure using exponential backoff, starting at 2 second delays for up to 6 retries. Retries will be performed if the method fails with an uncaught exception. | ||
|
|
||
| - This method can be `async`. | ||
|
|
||
| - Refer to [`alarm`](/durable-objects/api/alarms/#alarm) for more information. | ||
|
|
||
| ### `webSocketMessage` | ||
|
|
||
| - <code> webSocketMessage(ws <Type text="WebSocket" />, message{" "} <Type text="string | ArrayBuffer" />)</code>: <Type text="void" /> | ||
|
|
||
| - Called by the system when an accepted WebSocket receives a message. | ||
|
|
||
| - This method can be `async`. | ||
|
|
||
| - This method is not called for WebSocket control frames. The system will respond to an incoming [WebSocket protocol ping](https://www.rfc-editor.org/rfc/rfc6455#section-5.5.2) automatically without interrupting hibernation. | ||
|
|
||
| ### `webSocketClose` | ||
Oxyjun marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| - <code> webSocketClose(ws <Type text="WebSocket" />, code <Type text="number" />,reason <Type text="string" />, wasClean <Type text="boolean" />)</code>: <Type text="void" /> | ||
Oxyjun marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| - Called by the system when a WebSocket is closed. `wasClean()` is true if the connection closed cleanly, false otherwise. | ||
|
|
||
| - This method can be `async`. | ||
|
|
||
| ### `webSocketError` | ||
|
|
||
| - <code> webSocketError(ws <Type text="WebSocket" />, error <Type text="any" />)</code> : <Type text="void" /> | ||
|
|
||
| - Called by the system when any non-disconnection related errors occur. | ||
|
|
||
| - This method can be `async`. | ||
|
|
||
| ## Properties | ||
|
|
||
| ### `DurableObjectState` | ||
|
|
||
| See [`DurableObjectState` documentation](/durable-objects/api/state/). | ||
|
|
||
| ### `Env` | ||
|
|
||
| A list of bindings which are available to the Durable Object. | ||
|
|
||
| ## Related resources | ||
|
|
||
| - Refer to [Use WebSockets](/durable-objects/best-practices/websockets/) for more information on examples of WebSocket methods and best practices. | ||
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -2,7 +2,7 @@ | |
| title: SQL Storage | ||
| pcx_content_type: concept | ||
| sidebar: | ||
| order: 5 | ||
| order: 6 | ||
|
|
||
| --- | ||
|
|
||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -2,7 +2,7 @@ | |
| pcx_content_type: configuration | ||
| title: WebGPU | ||
| sidebar: | ||
| order: 8 | ||
| order: 9 | ||
| --- | ||
|
|
||
| :::caution | ||
|
|
||
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -5,7 +5,7 @@ sidebar: | |
| order: 5 | ||
| --- | ||
|
|
||
| import { Tabs, TabItem, GlossaryTooltip } from "~/components"; | ||
| import { Tabs, TabItem, GlossaryTooltip, Type } from "~/components"; | ||
|
|
||
| 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. | ||
|
|
||
|
|
@@ -375,11 +375,29 @@ A full example can be found in [Build a WebSocket server with WebSocket Hibernat | |
|
|
||
| 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/best-practices/websockets/#websocketmessage) will still be delivered, the Durable Object will never be evicted from memory. | ||
| If you are using older versions, note that while hibernatable WebSocket events such as [`webSocketMessage()`](/durable-objects/api/base/#websocketmessage) will still be delivered, the Durable Object will never be evicted from memory. | ||
|
|
||
| ::: | ||
|
|
||
| ## Extended methods | ||
|
|
||
| ### `serializeAttachment` | ||
|
|
||
| - <code> serializeAttachment(value <Type text="any" />)</code>: <Type text="void" /> | ||
|
|
||
| - Keeps a copy of `value` associated with the WebSocket to survive hibernation. The value can be any type supported by the [structured clone algorithm](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm), which is true of most types. If the value needs to be durable please use [Durable Object Storage](/durable-objects/api/storage-api/). | ||
|
|
||
| - If you modify `value` after calling this method, those changes will not be retained unless you call this method again. The serialized size of `value` is limited to 2,048 bytes, otherwise this method will throw an error. If you need larger values to survive hibernation, use the [Storage API](/durable-objects/api/storage-api/) and pass the corresponding key to this method so it can be retrieved later. | ||
|
|
||
| ### `deserializeAttachment` | ||
|
|
||
| - `deserializeAttachment()`: <Type text='any' /> | ||
|
|
||
| - Retrieves the most recent value passed to `serializeAttachment()`, or `null` if none exists. | ||
|
|
||
| ## Related resources | ||
|
|
||
| - [Mozilla Developer Network's (MDN) documentation on the WebSocket class](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket) | ||
| - [Cloudflare's WebSocket template for building applications on Workers using WebSockets](https://github.com/cloudflare/websocket-template) | ||
| - [Durable Object base class](/durable-objects/api/base/) | ||
| - [Durable Object State interface](/durable-objects/api/state/) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.