Skip to content
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/content/docs/durable-objects/api/alarms.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: Alarms
pcx_content_type: concept
sidebar:
order: 7
order: 8
---

import { Type, GlossaryTooltip } from "~/components";
Expand Down
86 changes: 86 additions & 0 deletions src/content/docs/durable-objects/api/base.mdx
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`

- <code> webSocketClose(ws <Type text="WebSocket" />, code <Type text="number" />,reason <Type text="string" />, wasClean <Type text="boolean" />)</code>: <Type text="void" />

- 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.
2 changes: 1 addition & 1 deletion src/content/docs/durable-objects/api/id.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: Durable Object ID
pcx_content_type: concept
sidebar:
order: 2
order: 3
---

import { Render, Tabs, TabItem, GlossaryTooltip } from "~/components";
Expand Down
2 changes: 1 addition & 1 deletion src/content/docs/durable-objects/api/namespace.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: Durable Object Namespace
pcx_content_type: concept
sidebar:
order: 1
order: 2
---

import { Render, Tabs, TabItem, GlossaryTooltip } from "~/components";
Expand Down
2 changes: 1 addition & 1 deletion src/content/docs/durable-objects/api/sql-storage.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: SQL Storage
pcx_content_type: concept
sidebar:
order: 5
order: 6

---

Expand Down
4 changes: 2 additions & 2 deletions src/content/docs/durable-objects/api/state.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
title: Durable Object State
pcx_content_type: concept
sidebar:
order: 4
order: 5
---

import { Tabs, TabItem, GlossaryTooltip } from "~/components";
import { Tabs, TabItem, GlossaryTooltip, Type, MetaInfo } from "~/components";

## Description

Expand Down
2 changes: 1 addition & 1 deletion src/content/docs/durable-objects/api/storage-api.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: Durable Object Storage
pcx_content_type: concept
sidebar:
order: 5
order: 6
---

import { Render, Type, MetaInfo, GlossaryTooltip } from "~/components";
Expand Down
2 changes: 1 addition & 1 deletion src/content/docs/durable-objects/api/stub.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: Durable Object Stub
pcx_content_type: concept
sidebar:
order: 3
order: 4
---

import { Render, GlossaryTooltip } from "~/components";
Expand Down
2 changes: 1 addition & 1 deletion src/content/docs/durable-objects/api/webgpu.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
pcx_content_type: configuration
title: WebGPU
sidebar:
order: 8
order: 9
---

:::caution
Expand Down
2 changes: 1 addition & 1 deletion src/content/docs/durable-objects/api/workers-rs.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ pcx_content_type: navigation
title: Rust API
external_link: https://github.com/cloudflare/workers-rs?tab=readme-ov-file#durable-objects
sidebar:
order: 10
order: 11

---
22 changes: 20 additions & 2 deletions src/content/docs/durable-objects/best-practices/websockets.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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/)
Loading