Skip to content

Conversation

MrgSub
Copy link
Contributor

@MrgSub MrgSub commented Oct 10, 2025

SyncAgent is a replica of Agent (without the MCP client) that has bidirectional sync capabilities while using the useAgent hook on the frontend to receive data and execute mutations.

Example in examples/todo-list demonstrates isolation for user data using this.sql

  • Perhaps this can be its own packages/sync or something
  • Docs written by AI agents are in the commits - feel free to squash or re-add if needed

MrgSub and others added 16 commits October 8, 2025 21:52
This document outlines a comprehensive design for implementing bi-directional
sync between Durable Objects (Agents) and React clients using dedicated hooks:
- useDurableQuery for subscribing to database queries
- useDurableMutation for executing mutations

Key features:
- Real-time synchronization across all connected clients
- Hibernation-aware with automatic re-subscription
- Query/mutation registry pattern on the server
- Type-safe client hooks
- SQL-based subscription persistence

The design leverages existing Agent infrastructure (SQL, WebSocket, broadcasting)
while adding a structured query/mutation layer similar to React Query/tRPC.
- Cursor-based pagination (recommended) with useDurableInfiniteQuery hook
- Offset-based pagination for traditional page navigation
- Complete implementation examples for both patterns
- Real-time update strategies for paginated data
- Subscription persistence details for multi-page scenarios
- Marked pagination question as resolved in discussion section
- Create comprehensive TanStack Query integration guide
- Update main design document to reference TanStack Query foundation
- Simplify hook implementations by leveraging TanStack Query
- Add benefits: caching, deduplication, optimistic updates, DevTools
- Include setup instructions and QueryClientProvider configuration
- Document migration path and advanced patterns

Key improvements:
- Built on battle-tested React Query infrastructure
- Automatic request deduplication and caching
- Rich DevTools for debugging
- Optimistic updates with rollback support
- Framework-agnostic core for future extensions
- Support both local SQLite (10GB limit) and D1 (shared database)
- Use Drizzle ORM for type-safe database operations
- Automatic userId filtering for shared databases (D1)
- No filtering needed for local SQLite (already isolated per DO)
- userId filter uses this.name from Durable Object
- Complete schema definition and configuration examples
- Storage limits table and migration recommendations
- Dependencies: drizzle-orm, drizzle-kit

Key features:
- Flexible storage backend selection via environment variable
- Type-safe queries with Drizzle's inferSelect/inferInsert
- Automatic multi-tenancy support for D1
- Same query API regardless of backend
- Implements a classic TodoMVC-style todo list application
- Demonstrates real-time state synchronization using Cloudflare Agents sync layer
- Features:
  - Add, edit, complete, and delete todos
  - Filter by all, active, or completed todos
  - Clear all completed todos
  - Toggle all todos at once
  - Real-time state sync across multiple browser windows
- Uses useAgent hook for seamless client-server state management
- All state changes via setState() automatically broadcast to connected clients
- Includes comprehensive README with usage instructions
- Add 15 tests covering all TodoAgent functionality
- Test state management, todo operations, filters, and persistence
- Add getState() method to TodoAgent for test access
- Configure Vitest with proper Wrangler test setup
- All tests passing successfully
- Add client.test.tsx with 5 comprehensive React test cases
- Test state initialization and rendering
- Test state updates via onStateUpdate callback
- Test agent.call() for add, toggle, delete, and filter operations
- Test client-side filtering logic
- Add vitest.react.config.ts for browser-based testing with Playwright
- All tests passing successfully
…mple

Add Todo List example demonstrating sync layer
- Add new MessageType enums for query/mutation operations
- Create QuerySubscriptionManager for subscription tracking
- Add registerQuery and registerMutation methods to Agent class
- Implement query execution and broadcasting with versioning
- Add message handlers for subscribe/unsubscribe/mutation
- Create React hooks: useDurableQuery, useDurableMutation, useDurableInfiniteQuery
- Update todo-list example to use new query/mutation sync system
- Add SQL tables for subscriptions, query updates, and mutations
…tion-sync

Research: Bi-directional Sync with useDurableQuery and useDurableMutation Hooks
Copy link

changeset-bot bot commented Oct 10, 2025

⚠️ No Changeset found

Latest commit: 4f6c344

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link

pkg-pr-new bot commented Oct 11, 2025

Open in StackBlitz

npm i https://pkg.pr.new/cloudflare/agents@557

commit: 4f6c344

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant