Skip to content

Commit 7fe83c9

Browse files
[DO] Adding Durable Object base class (#18423)
* Adding a new * Adding Durable Object Base Class chapter. Adding cross-references. * Adding link to related resources. * Adding extended methods to DO State page. * Adding fetch and alarm. Actioning PR comments. * Revert "Adding fetch and alarm. Actioning PR comments." This reverts commit 6088c07. * Adding fetch and alarm to base class chapter. Actioning other PR comments. * Adding alarm handler method into DO base class. * Importing missing package. * Adding missing component x2 * Clarifying language on serializeAttachment method. * Apply suggestions from code review Co-authored-by: marciocloudflare <[email protected]> --------- Co-authored-by: marciocloudflare <[email protected]>
1 parent 01c3e61 commit 7fe83c9

File tree

11 files changed

+116
-12
lines changed

11 files changed

+116
-12
lines changed

src/content/docs/durable-objects/api/alarms.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Alarms
33
pcx_content_type: concept
44
sidebar:
5-
order: 7
5+
order: 8
66
---
77

88
import { Type, GlossaryTooltip } from "~/components";
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
---
2+
title: Durable Object Base Class
3+
pcx_content_type: concept
4+
sidebar:
5+
order: 1
6+
---
7+
8+
import { Render, Tabs, TabItem, GlossaryTooltip, Type, MetaInfo, TypeScriptExample } from "~/components";
9+
10+
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.
11+
12+
<TypeScriptExample>
13+
```ts
14+
export class MyDurableObject extends DurableObject {
15+
constructor(ctx: DurableObjectState, env: Env) {
16+
super(ctx, env);
17+
}
18+
19+
async fetch(request: Request) {
20+
return new Response("Hello, World!");
21+
}
22+
}
23+
```
24+
</TypeScriptExample>
25+
26+
## Methods
27+
28+
### `fetch`
29+
30+
- <code>fetch(<Type text="Request"/>)</code>: <Type text="Response"/> | <Type text="Promise <Response>"/>
31+
32+
- 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.
33+
34+
- This method can be `async`.
35+
36+
### `alarm`
37+
38+
- <code>alarm()</code>: <Type text="Promise <void>"/>
39+
40+
- Called by the system when a scheduled alarm time is reached.
41+
42+
- The `alarm()` handler has guaranteed at-least-once execution and will be retried upon failure using exponential backoff, starting at two second delays for up to six retries. Retries will be performed if the method fails with an uncaught exception.
43+
44+
- This method can be `async`.
45+
46+
- Refer to [`alarm`](/durable-objects/api/alarms/#alarm) for more information.
47+
48+
### `webSocketMessage`
49+
50+
- <code> webSocketMessage(ws <Type text="WebSocket" />, message{" "} <Type text="string | ArrayBuffer" />)</code>: <Type text="void" />
51+
52+
- Called by the system when an accepted WebSocket receives a message.
53+
54+
- This method can be `async`.
55+
56+
- 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.
57+
58+
### `webSocketClose`
59+
60+
- <code> webSocketClose(ws <Type text="WebSocket" />, code <Type text="number" />, reason <Type text="string" />, wasClean <Type text="boolean" />)</code>: <Type text="void" />
61+
62+
- Called by the system when a WebSocket is closed. `wasClean()` is true if the connection closed cleanly, false otherwise.
63+
64+
- This method can be `async`.
65+
66+
### `webSocketError`
67+
68+
- <code> webSocketError(ws <Type text="WebSocket" />, error <Type text="any" />)</code> : <Type text="void" />
69+
70+
- Called by the system when any non-disconnection related errors occur.
71+
72+
- This method can be `async`.
73+
74+
## Properties
75+
76+
### `DurableObjectState`
77+
78+
See [`DurableObjectState` documentation](/durable-objects/api/state/).
79+
80+
### `Env`
81+
82+
A list of bindings which are available to the Durable Object.
83+
84+
## Related resources
85+
86+
- Refer to [Use WebSockets](/durable-objects/best-practices/websockets/) for more information on examples of WebSocket methods and best practices.

src/content/docs/durable-objects/api/id.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Durable Object ID
33
pcx_content_type: concept
44
sidebar:
5-
order: 2
5+
order: 3
66
---
77

88
import { Render, Tabs, TabItem, GlossaryTooltip } from "~/components";

src/content/docs/durable-objects/api/namespace.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Durable Object Namespace
33
pcx_content_type: concept
44
sidebar:
5-
order: 1
5+
order: 2
66
---
77

88
import { Render, Tabs, TabItem, GlossaryTooltip } from "~/components";

src/content/docs/durable-objects/api/sql-storage.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: SQL Storage
33
pcx_content_type: concept
44
sidebar:
5-
order: 5
5+
order: 6
66

77
---
88

src/content/docs/durable-objects/api/state.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
title: Durable Object State
33
pcx_content_type: concept
44
sidebar:
5-
order: 4
5+
order: 5
66
---
77

8-
import { Tabs, TabItem, GlossaryTooltip } from "~/components";
8+
import { Tabs, TabItem, GlossaryTooltip, Type, MetaInfo } from "~/components";
99

1010
## Description
1111

src/content/docs/durable-objects/api/storage-api.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Durable Object Storage
33
pcx_content_type: concept
44
sidebar:
5-
order: 5
5+
order: 6
66
---
77

88
import { Render, Type, MetaInfo, GlossaryTooltip } from "~/components";

src/content/docs/durable-objects/api/stub.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Durable Object Stub
33
pcx_content_type: concept
44
sidebar:
5-
order: 3
5+
order: 4
66
---
77

88
import { Render, GlossaryTooltip } from "~/components";

src/content/docs/durable-objects/api/webgpu.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
pcx_content_type: configuration
33
title: WebGPU
44
sidebar:
5-
order: 8
5+
order: 9
66
---
77

88
:::caution

src/content/docs/durable-objects/api/workers-rs.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ pcx_content_type: navigation
33
title: Rust API
44
external_link: https://github.com/cloudflare/workers-rs?tab=readme-ov-file#durable-objects
55
sidebar:
6-
order: 10
6+
order: 11
77

88
---

0 commit comments

Comments
 (0)