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 setting
core-transactions — transact() bundling, origin parameter, event ordering
core-document-updates — encodeStateAsUpdate, applyUpdate, mergeUpdates, state vectors
core-garbage-collection — gc flag, when to disable for version history
Shared Types (CRITICAL)
types-ytext — Text & RichText, insert/format/delta, Quill compatibility
types-yarray — Ordered sequences, insert/delete/push, nested types
types-ymap — Key-value storage, observe, historical key retention gotcha
types-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 setup
provider-webrtc — y-webrtc peer-to-peer connections
provider-indexeddb — y-indexeddb offline persistence
provider-custom — Building custom providers, update properties
Editor Bindings (HIGH)
binding-tiptap — y-prosemirror with TipTap
binding-prosemirror — y-prosemirror directly
binding-codemirror — y-codemirror.next
binding-quill — y-quill
binding-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 sync
pitfall-subdocuments — When to use vs Y.Map alternatives
pitfall-v2-encoding — useV2Encoding experimental flag
debug-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
1---2name: yjs-best-practices3description: 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'.4license: MIT5---67# Yjs Best Practices89Comprehensive 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.1011## When to Apply1213Reference these guidelines when:14- Creating or managing Y.Doc instances for collaborative documents15- Using shared types (Y.Text, Y.Array, Y.Map, Y.XmlFragment) for synced data16- Setting up network providers (y-websocket, y-webrtc) or persistence (y-indexeddb)17- Integrating Yjs with editors (TipTap, ProseMirror, CodeMirror, Quill, Monaco)18- Implementing user presence and cursor awareness19- Adding undo/redo to collaborative editing20- Debugging sync issues, state vector mismatches, or update encoding2122## Rule Categories by Priority2324| Priority | Category | Impact | Prefix |25|----------|----------|--------|--------|26| 1 | Core (Y.Doc & Updates) | CRITICAL | `core-` |27| 2 | Shared Types | CRITICAL | `types-` |28| 3 | Providers | HIGH | `provider-` |29| 4 | Editor Bindings | HIGH | `binding-` |30| 5 | Awareness | MEDIUM-HIGH | `awareness-` |31| 6 | Undo/Redo | MEDIUM | `undo-` |32| 7 | Pitfalls & Debugging | MEDIUM | `pitfall-`, `debug-` |3334## Quick Reference3536### Core (CRITICAL)37- `core-ydoc-setup` — Y.Doc creation, clientID, gc setting38- `core-transactions` — transact() bundling, origin parameter, event ordering39- `core-document-updates` — encodeStateAsUpdate, applyUpdate, mergeUpdates, state vectors40- `core-garbage-collection` — gc flag, when to disable for version history4142### Shared Types (CRITICAL)43- `types-ytext` — Text & RichText, insert/format/delta, Quill compatibility44- `types-yarray` — Ordered sequences, insert/delete/push, nested types45- `types-ymap` — Key-value storage, observe, historical key retention gotcha46- `types-ymap-ykeyvalue` — YKeyValue from y-utility for efficient key-value (524KB → 271B)47- `types-xml` — Y.XmlFragment/Element/Text for structured documents4849### Providers (HIGH)50- `provider-websocket` — y-websocket client and server setup51- `provider-webrtc` — y-webrtc peer-to-peer connections52- `provider-indexeddb` — y-indexeddb offline persistence53- `provider-custom` — Building custom providers, update properties5455### Editor Bindings (HIGH)56- `binding-tiptap` — y-prosemirror with TipTap57- `binding-prosemirror` — y-prosemirror directly58- `binding-codemirror` — y-codemirror.next59- `binding-quill` — y-quill60- `binding-monaco` — y-monaco6162### Awareness (MEDIUM-HIGH)63- `awareness-protocol` — Presence, cursors, setLocalStateField6465### Undo/Redo (MEDIUM)66- `undo-manager` — Y.UndoManager, captureTimeout, trackedOrigins, cursor restoration6768### Pitfalls & Debugging (MEDIUM)69- `pitfall-duplicate-imports` — CJS/ESM double-import breaks sync70- `pitfall-subdocuments` — When to use vs Y.Map alternatives71- `pitfall-v2-encoding` — useV2Encoding experimental flag72- `debug-common-issues` — logUpdate, state inspection, connection debugging7374## How to Use7576Read individual rule files for detailed explanations and code examples:7778```79rules/core/core-ydoc-setup.md80rules/shared-types/types-ytext.md81rules/providers/provider-websocket.md82```8384Each rule file contains:85- Brief explanation of why it matters86- Correct code example with explanation87- Verification checklist88- Source pointers to official docs8990## Compiled Documents9192- `AGENTS.md` — Compressed index of all rules with file paths (start here)93- `AGENTS.full.md` — Full verbose guide with all rules expanded inline