Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
46 changes: 46 additions & 0 deletions src/content/docs/durable-objects/api/base.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
title: Durable Object Base Class
pcx_content_type: concept
sidebar:
order: 1
---

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

{/* DEFINITION OF DO BASE CLASS TBC */}

## Methods

### `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

{/* Properties of DO Base Class TBC */}

## 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
20 changes: 18 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 Expand Up @@ -267,6 +267,22 @@ export class MyDurableObject extends DurableObject {

- None.

## Extended methods

### `serializeAttachment`

- <code> serializeAttachment(value <Type text="any" />)</code>: <Type text="void" />

- Keeps a copy of `value` in memory (not on disk) 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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keeps a copy of value in memory (not on disk)
@MellowYarker does a user need to worry about the case where the runtime crashes and potentially loses the in-memory copy? Does this count against the memory consumed by the Durable Object?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does a user need to worry about the case where the runtime crashes and potentially loses the in-memory copy

Yes, this will not survive a "hard" eviction. The only way this survives eviction is if we do a "soft" eviction (no requests for 10 seconds).

Does this count against the memory consumed by the Durable Object?

Not when the DO is hibernating. When the DO is awake and the WebSocket is in memory (it was just created, or received a message, or was brought back in via state.getWebSockets() it does contribute (because it lives in the isolate).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Oxyjun I would add that

Keeps a copy of value associated with the WebSocket to survive hibernation. The value can be any type supported by the structured clone algorithm, which is true of most types. If the value needs to be durable please use DurableObjectStorage (hyperlink the Durable Object storage reference)


- 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.

## Properties

### `id`
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

---
Original file line number Diff line number Diff line change
Expand Up @@ -375,11 +375,13 @@ 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.

:::

## 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