name: yjs-best-practices description: "Yjs CRDT best practices for building real-time collaborative applications. Use this skill when implementing collaborative editing, real-time sync, shared data structures, or any feature using Yjs (Y.Doc, Y.Text, Y.Array, Y.Map, Y.XmlFragment). Triggers on tasks involving Yjs, CRDTs, collaborative editors, y-websocket, y-webrtc, y-indexeddb, y-prosemirror, y-codemirror, y-quill, y-monaco, awareness/presence, undo/redo with shared types, or any mention of real-time document collaboration — even if the user doesn't explicitly say 'Yjs'." license: MIT metadata: author: velt version: "1.1.0"
Yjs Best Practices
Comprehensive guide for implementing real-time collaborative applications with Yjs — the high-performance CRDT framework. Contains 24 rules across 8 categories covering document lifecycle, shared types, providers, editor bindings, awareness, undo/redo, common pitfalls, and debugging.
When to Apply
Reference these guidelines when:
- Creating or managing Y.Doc instances for collaborative documents
- Using shared types (Y.Text, Y.Array, Y.Map, Y.XmlFragment) for synced data
- Setting up network providers (y-websocket, y-webrtc) or persistence (y-indexeddb)
- Integrating Yjs with editors (TipTap, ProseMirror, CodeMirror, Quill, Monaco)
- Implementing user presence and cursor awareness
- Adding undo/redo to collaborative editing
- Debugging sync issues, state vector mismatches, or update encoding
Rule Categories by Priority
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Core (Y.Doc & Updates) | CRITICAL | core- |
| 2 | Shared Types | CRITICAL | types- |
| 3 | Providers | HIGH | provider- |
| 4 | Editor Bindings | HIGH | binding- |
| 5 | Awareness | MEDIUM-HIGH | awareness- |
| 6 | Undo/Redo | MEDIUM | undo- |
| 7 | Pitfalls & Debugging | MEDIUM | pitfall-, debug- |
Quick Reference
Core (CRITICAL)
core-ydoc-setup— Y.Doc creation, clientID, gc settingcore-transactions— transact() bundling, origin parameter, event orderingcore-document-updates— encodeStateAsUpdate, applyUpdate, mergeUpdates, state vectorscore-garbage-collection— gc flag, when to disable for version history
Shared Types (CRITICAL)
types-ytext— Text & RichText, insert/format/delta, Quill compatibilitytypes-yarray— Ordered sequences, insert/delete/push, nested typestypes-ymap— Key-value storage, observe, historical key retention gotchatypes-ymap-ykeyvalue— YKeyValue from y-utility for efficient key-value (524KB → 271B)types-xml— Y.XmlFragment/Element/Text for structured documents
Providers (HIGH)
provider-websocket— y-websocket client and server setupprovider-webrtc— y-webrtc peer-to-peer connectionsprovider-indexeddb— y-indexeddb offline persistenceprovider-custom— Building custom providers, update properties
Editor Bindings (HIGH)
binding-tiptap— y-prosemirror with TipTapbinding-prosemirror— y-prosemirror directlybinding-codemirror— y-codemirror.nextbinding-quill— y-quillbinding-monaco— y-monaco
Awareness (MEDIUM-HIGH)
awareness-protocol— Presence, cursors, setLocalStateField
Undo/Redo (MEDIUM)
undo-manager— Y.UndoManager, captureTimeout, trackedOrigins, cursor restoration
Pitfalls & Debugging (MEDIUM)
pitfall-duplicate-imports— CJS/ESM double-import breaks syncpitfall-subdocuments— When to use vs Y.Map alternativespitfall-v2-encoding— useV2Encoding experimental flagdebug-common-issues— logUpdate, state inspection, connection debugging
How to Use
Read individual rule files for detailed explanations and code examples:
rules/core/core-ydoc-setup.md
rules/shared-types/types-ytext.md
rules/providers/provider-websocket.md
Each rule file contains:
- Brief explanation of why it matters
- Correct code example with explanation
- Verification checklist
- Source pointers to official docs
Compiled Documents
AGENTS.md— Compressed index of all rules with file paths (start here)AGENTS.full.md— Full verbose guide with all rules expanded inline