|
| 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. |
0 commit comments