Sign In
Hosting Providers

Cloudflare Workers

Deploy Rivet Actors to Cloudflare Workers with Durable Objects for global edge computing with persistent state.

Feature Support

FeatureSupported
Horizontal scalingYes
WebSocketsYes
SSEYes
EdgeYes
SchedulingYes

Setup

Install packages

Install the Cloudflare Workers driver:

Command Line
npm install @rivetkit/cloudflare-workers

Configure the driver

Update your server code to support Cloudflare Workers:

server.ts
import { createServer } from "@rivetkit/cloudflare-workers";
import { Hono } from "hono";
import { registry } from "./registry";

const { client, createHandler } = createServer(registry);

// Setup router
const app = new Hono();

// Example API endpoint
app.post("/increment/:name", async (c) => {
  const name = c.req.param("name");

  // Get or create actor and call action
  const counter = client.counter.getOrCreate(name);
  const newCount = await counter.increment(1);

  return c.json({ count: newCount });
});

const { handler, ActorHandler } = createHandler(app);

export { handler as default, ActorHandler };

Configure Wrangler

Update your wrangler.json configuration to support ACTOR_DO and ACTOR_KV bindings:

wrangler.json
{
  "name": "my-rivetkit-app",
  "main": "src/index.ts",
  "compatibility_date": "2025-01-20",
  "compatibility_flags": ["nodejs_compat"],
  "migrations": [
    {
      "tag": "v1",
      "new_classes": ["ActorHandler"]
    }
  ],
  "durable_objects": {
    "bindings": [
      {
        "name": "ACTOR_DO",
        "class_name": "ActorHandler"
      }
    ]
  },
  "kv_namespaces": [
    {
      "binding": "ACTOR_KV",
      "id": "your_namespace_id"
    }
  ]
}

Configuration Requirements:

  • ACTOR_DO - Durable Object binding for actor persistence
  • ACTOR_KV - KV namespace binding for metadata storage
  • nodejs_compat - Required compatibility flag
  • Migration with ActorHandler class definition

Deploy

Deploy your application to Cloudflare Workers:

Command Line
wrangler deploy

Your actors will now run on Cloudflare's global edge network with persistent state backed by Durable Objects.

Examples

Advanced

Accessing Environment Bindings

You can access Cloudflare Workers environment bindings directly using the importable env:

TypeScript
import { env } from "cloudflare:workers";

// Access environment variables and secrets in top-level scope
const API_KEY = env.API_KEY;
const LOG_LEVEL = env.LOG_LEVEL || "info";

// Use bindings in your actor
const myActor = actor({
  state: { count: 0 },
  
  actions: {
    // Access KV, D1, or other bindings during request handling
    getFromKV: async (c, key: string) => {
      // Access additional KV namespaces defined in wrangler.json
      if (env.MY_CACHE_KV) {
        return await env.MY_CACHE_KV.get(key);
      }
    }
  }
});

Driver Context

The Cloudflare Workers driver provides access to the Durable Object state and environment through the driver context in createVars.

TypeScript
import { actor, ActorInitContext } from "@rivetkit/actor";
import type { DriverContext } from "@rivetkit/cloudflare-workers";

const myActor = actor({
  state: { count: 0 },
  
  // Save the Cloudflare driver context
  createVars: (ctx: ActorInitContext, driver: DriverContext) => ({ 
    state: driver.state,
  }),
  
  actions: {
    // Example: Access Durable Object info (not recommended in practice)
    kvGet: (c, key: string) => {
      const doState = c.vars.state;
	  return await doState.storage.get(key)
    },
  }
});

The Cloudflare Workers driver context type is exported as DriverContext from @rivetkit/cloudflare-workers:

TypeScript
interface DriverContext {
  state: DurableObjectState;
}

While you have access to the Durable Object state, be cautious when directly modifying KV storage or alarms, as this may interfere with RivetKit's internal operations and potentially break actor functionality.

Suggest changes to this page