Documentation

Cloudflare Durable Objects & Workflows

ready

Strongly consistent state, coordinated WebSockets, embedded SQLite edge actors, and durable multi-step Cloudflare Workflows exported directly from your Next.js Worker.

Source reference: cli/internal/serverless/cloudflare/adapter/extensions.go, cli/internal/serverless/cloudflare/workflows.go


Overview

Durable Objects provide low-latency, globally coordinated, strongly consistent in-memory state and embedded SQLite storage — ideal for multiplayer collaboration, live chat rooms, stateful AI agent sessions, and WebSockets. Cloudflare Workflows provide durable, multi-step background execution with automatic retries and sleep/wait primitives.

NextDeploy compiles and re-exports your self-hosted Durable Object and Workflow classes right inside worker.mjs via serverless.cloudflare.extensions, verifies that every declared class_name is exported before upload, and applies SQLite class migrations automatically.


Configuration & Class Migrations

Declare extensions, bindings.durable_objects (and/or bindings.workflows), and chronological migrations in nextdeploy.yml:

version: "1.0"
target_type: serverless

app:
  name: my-realtime-app
  domain: chat.example.com
  environment: production

serverless:
  provider: cloudflare
  cloudflare:
    compatibility_date: "2026-05-01"
    compatibility_flags:
      - nodejs_compat
    extensions:
      - ./worker/chat-room.ts
    bindings:
      durable_objects:
        - name: CHAT_ROOM
          class_name: ChatRoom
    migrations:
      - tag: v1
        new_sqlite_classes: ["ChatRoom"]

Implementing a Durable Object

Export your actor class from the extension module referenced in extensions:

// worker/chat-room.ts
export class ChatRoom {
  state: DurableObjectState;
  sql: SqlStorage;

  constructor(state: DurableObjectState) {
    this.state = state;
    this.sql = state.storage.sql;
    this.sql.exec(`
      CREATE TABLE IF NOT EXISTS messages (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        user TEXT,
        text TEXT,
        created_at DATETIME DEFAULT CURRENT_TIMESTAMP
      )
    `);
  }

  async fetch(request: Request) {
    if (request.headers.get('Upgrade') === 'websocket') {
      const pair = new WebSocketPair();
      this.state.acceptWebSocket(pair[1]);
      return new Response(null, { status: 101, webSocket: pair[0] });
    }

    if (request.method === 'POST') {
      const { user, text } = await request.json();
      this.sql.exec('INSERT INTO messages (user, text) VALUES (?, ?)', user, text);
      return Response.json({ success: true });
    }

    const cursor = this.sql.exec('SELECT * FROM messages ORDER BY id DESC LIMIT 50');
    return Response.json(cursor.toArray());
  }
}

Accessing Durable Objects from Next.js

Route user requests to the coordinated singleton instance via globalThis.__nextdeployEnv:

// app/api/chat/route.ts
import { NextRequest, NextResponse } from 'next/server';

export async function GET(req: NextRequest) {
  const env = (globalThis as { __nextdeployEnv?: CloudflareEnv }).__nextdeployEnv;
  const namespace = env?.CHAT_ROOM;
  if (!namespace) {
    return NextResponse.json({ error: 'CHAT_ROOM binding not configured' }, { status: 500 });
  }

  const id = namespace.idFromName('room-general');
  const stub = namespace.get(id);
  const res = await stub.fetch(req.url);
  return NextResponse.json(await res.json());
}

Related