- Home
- Skills
- APIs & Backend
- Yjs Multiplayer + Offline Sync for Canvas Apps
Yjs Multiplayer + Offline Sync for Canvas Apps
Building collaborative features is notoriously difficult.
$19
Works with the AI Agents you already use
Yjs Multiplayer + Offline Sync for Canvas Apps
Example session with this skill installed
Make my drawing app collaborative. It uses React and saves strokes as a JSON array in local state. I want live cursors and offline support.
- Read your context and instructions
- Compiled the yjs multiplayer
I have mapped your drawing state to a Yjs document and set up the connection logic. The implementation uses y-indexeddb for offline persistence and y-websocket for the sync layer. I've also added a cursor overlay component that tracks positions in canvas coordinates to ensure they stay accurate regardless of zoom level.
Multiplayer Canvas Integration
| Feature | Implementation Detail | Status |
|---|---|---|
| Document Type | Y.Map<Y.Map<any>> (Keyed by UUID) | Configured |
| Offline Layer | IndexeddbPersistence (Prefixed by Room ID) | Active |
| Presence | Awareness API with 30s timeout | Enabled |
| Persistence | Postgres Update Log with Snapshot Compaction | Ready |
| Input | touch-action: none with Pointer Events | Optimized |
// src/hooks/useRoom.ts
import * as Y from 'yjs';
import { WebsocketProvider } from 'y-websocket';
import { IndexeddbPersistence } from 'y-indexeddb';
export function useRoom(roomId: string) {
const doc = useMemo(() => new Y.Doc(), []);
useEffect(() => {
const ldb = new IndexeddbPersistence(`draw-app:${roomId}`, doc);
const provider = new WebsocketProvider('wss://your-sync-server.com', roomId, doc, { connect: false });
// Auth and Connection logic
provider.connect();
return () => {
provider.destroy();
ldb.destroy();
doc.destroy();
};
}, [roomId, doc]);
return { doc, provider };
}
Next steps
- Deploy the Node.js sync server using the provided
templates/server.tsto a host like Railway or Fly.io. - Update your
Canvascomponent to subscribe todoc.getMap('shapes').observeDeep()to trigger re-renders. - Add the
ROOM_TOKEN_SECRETto your environment variables to enable JWT signing.
Connects securely to your tools. The creator never sees your data.
About this skill
Building collaborative features is notoriously difficult. Handling CRDT merges, managing WebSocket lifecycles, and ensuring offline edits don't vanish on reconnect takes weeks of tuning. This skill implements a production-grade synchronization layer for web canvases and editors.
What it does
- Multiplayer state: Implements Yjs CRDTs to sync shapes, strokes, and text across users with automatic conflict resolution.
- Offline-first sync: Configures IndexedDB persistence to ensure data is saved locally and merged the moment a connection returns.
- Presence & cursors: Adds real-time awareness for live cursors and "who is online" states using world-coordinate tracking to prevent zoom-drift.
- Secure sync server: Sets up a Node.js WebSocket server with JWT authentication and Postgres persistence for long-term storage.
- Input handling: Resolves common mobile issues like palm rejection, pinch-to-zoom on canvases, and smooth Apple Pencil input.
How it works
- State mapping: The skill analyzes your existing state (Zustand, Redux, or JSON) and maps it to shared Y.Map or Y.Text types.
- Infrastructure setup: It scaffolds the WebSocket provider, IndexedDB layer, and the Node.js backend with Postgres update-log logic.
- Integration: It wires the connection lifecycle into your React/Next.js components, including proper cleanup to prevent memory leaks.
- Mobile optimization: It applies CSS and pointer event listeners to handle touch-action and multi-touch gestures.
Frameworks & tools
TypeScript, React/Next.js, Node.js, Yjs, y-websocket, y-indexeddb, ws, PostgreSQL, and jose for JWT handling.
Why this beats prompting it yourself
Generic prompts often fail to handle the complex lifecycle of WebSockets in React, leading to duplicate connections or "ghost" objects. This skill enforces a specific, battle-tested order of operations for CRDT merging and Postgres compaction that prevents document corruption and memory bloat.
Use cases
- Adding Figma-like live collaboration to a whiteboard or canvas app.
- Building an offline-capable Markdown editor that syncs when back online.
- Replacing high-cost managed sync services with a self-hosted Yjs architecture.
- Fixing broken touch and zoom interactions on collaborative drawing tools.
Known limitations
Does not support serverless environments (Vercel/Netlify) for the sync server. Not intended for simple CRUD or chat-only applications. Requires a long-running process host.
Requires a long-running Node.js process for WebSockets (no serverless). Does not support chat-only apps. Requires manual hosting setup for Postgres and the sync server.
How to use
- 1
Connect your agent to the Agensi MCP
Copy the prompt and paste it into Claude, ChatGPT, Cursor or any agent that supports MCP. It sets up the connection for you. Manual setup
- 2
Sign in to Agensi
Your agent shows a sign-in link. Sign in and click Allow.
- 3
Ask for what you need
Your agent loads this skill and uses it on your task. It always asks before any payment.
Reviews
No reviews yet
Be one of the first to try it. Every listed skill goes through the trust checks below.
Security scanned
Checked by our 8-point scan before listing
Fresh listing
Recently published to Agensi
Money-back guarantee
Doesn't do what its page promises? You get your money back.
Trust & safety
Security scan
Security scan passed 15 days ago
Permissions
Allowed Hosts