|
| 1 | +--- |
| 2 | +title: "@cloudflare/actors library - SDK for Durable Objects in beta" |
| 3 | +description: "@cloudflare/actors library with Durable Objects helpers and patterns." |
| 4 | +products: |
| 5 | + - durable-objects |
| 6 | + - workers |
| 7 | +date: 2025-06-25T06:00:00Z |
| 8 | +--- |
| 9 | + |
| 10 | +The new [@cloudflare/actors](https://www.npmjs.com/package/@cloudflare/actors) library is now in beta! |
| 11 | + |
| 12 | +The `@cloudflare/actors` library is a new SDK for Durable Objects and provides a powerful set of abstractions for building real-time, interactive, and multiplayer applications on top of Durable Objects. With beta uasge and feedback, `@cloudflare/actors` will become the recommended way to build on Durable Objects and draws upon Cloudflare's experience building products/features on Durable Objects. |
| 13 | + |
| 14 | +The name "actors" originates from the [actor programming model](/durable-objects/what-are-durable-objects/#actor-programming-model), which closely ties to how Durable Objects are modelled. |
| 15 | + |
| 16 | +The `@cloudflare/actors` library includes: |
| 17 | + |
| 18 | +* Storage helpers for querying embeddeded, per-object SQLite storage |
| 19 | +* Storage helpers for managing SQL schema migrations |
| 20 | +* Alarm helpers for scheduling multiple alarms provided a date, delay in seconds, or cron expression |
| 21 | +* `Actor` class for using Durable Objects with a defined pattern |
| 22 | +* Durable Objects [Workers API](https://developers.cloudflare.com/durable-objects/api/base/) is always available for your application as needed |
| 23 | + |
| 24 | +Storage and alarm helper methods can be combined with [any Javascript class](https://github.com/cloudflare/actors?tab=readme-ov-file#storage--alarms-with-durableobject-class) that defines your Durable Object, i.e, ones that extend `DurableObject` including the `Actor` class. |
| 25 | + |
| 26 | +```js |
| 27 | +import { Storage } from "@cloudflare/actors/storage"; |
| 28 | + |
| 29 | +export class ChatRoom extends DurableObject<Env> { |
| 30 | + storage: Storage; |
| 31 | + |
| 32 | + constructor(ctx: DurableObjectState, env: Env) { |
| 33 | + super(ctx, env) |
| 34 | + this.storage = new Storage(ctx.storage); |
| 35 | + this.storage.migrations = [{ |
| 36 | + idMonotonicInc: 1, |
| 37 | + description: "Create users table", |
| 38 | + sql: "CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY)" |
| 39 | + }] |
| 40 | + } |
| 41 | + async fetch(request: Request): Promise<Response> { |
| 42 | + // Run migrations before executing SQL query |
| 43 | + await this.storage.runMigrations(); |
| 44 | + |
| 45 | + // Query with SQL template |
| 46 | + let userId = new URL(request.url).searchParams.get("userId"); |
| 47 | + const query = this.storage.sql`SELECT * FROM users WHERE id = ${userId};` |
| 48 | + return new Response(`${JSON.stringify(query)}`); |
| 49 | + } |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +`@cloudflare/actors` library introduces the `Actor` class pattern. `Actor` lets you access Durable Objects without writing the Worker that communicates with your Durable Object (the Worker is created for you). By default, requests are routed to a Durable Object named "default". |
| 54 | + |
| 55 | +```js |
| 56 | +export class MyActor extends Actor<Env> { |
| 57 | + async fetch(request: Request): Promise<Response> { |
| 58 | + return new Response('Hello, World!') |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +export default handler(MyActor); |
| 63 | +``` |
| 64 | + |
| 65 | +You can [route](/durable-objects/get-started/#3-instantiate-and-communicate-with-a-durable-object) to different Durable Objects by name within your `Actor` class using [`nameFromRequest`](https://github.com/cloudflare/actors?tab=readme-ov-file#actor-with-custom-name). |
| 66 | + |
| 67 | +```js |
| 68 | +export class MyActor extends Actor<Env> { |
| 69 | + static nameFromRequest(request: Request): string { |
| 70 | + let url = new URL(request.url); |
| 71 | + return url.searchParams.get("userId") ?? "foo"; |
| 72 | + } |
| 73 | + |
| 74 | + async fetch(request: Request): Promise<Response> { |
| 75 | + return new Response(`Actor identifier (Durable Object name): ${this.identifier}`); |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +export default handler(MyActor); |
| 80 | +``` |
| 81 | +
|
| 82 | +For more examples, check out the library [README](https://github.com/cloudflare/actors?tab=readme-ov-file#getting-started). `@cloudflare/actors` library is a place for more helpers and built-in patterns, like retry handling and Websocket-based applications, to reduce development overhead for common Durable Objects functionality. Please share feedback and what more you would like to see on our [Discord channel](https://discord.com/channels/595317990191398933/773219443911819284). |
0 commit comments