--- name: real-time-collaboration-engine description: Build real-time collaborative editing with WebSockets, OT/CRDT conflict resolution, and presence awareness. Implements cursor tracking, optimistic updates, and offline sync. Use for collaborative editors, whiteboards, video editing. Activate on "real-time collaboration", "WebSocket sync", "multiplayer editing", "CRDT", "presence awareness". NOT for simple chat, request-response APIs, or single-user apps. allowed-tools: Read,Write,Edit,Bash(npm:*,websocket:*) metadata: category: DevOps & Site Reliability tags: - real - time - collaboration - real-time-collaboration - websocket-sync pairs-with: - skill: websocket-streaming reason: WebSocket transport is the foundation for real-time collaborative editing connections - skill: react-performance-optimizer reason: Optimistic updates and cursor rendering require React performance optimization - skill: security-auditor reason: Collaborative editing requires access control and input sanitization security auditing --- # Real-Time Collaboration Engine Expert in building Google Docs-style collaborative editing with WebSockets, conflict resolution, and presence awareness. ## When to Use ✅ **Use for**: - Collaborative text/code editors - Shared whiteboards and design tools - Multi-user video editing timelines - Real-time data dashboards - Multiplayer game state sync ❌ **NOT for**: - Simple chat applications (use basic WebSocket) - Request-response APIs (use REST/GraphQL) - Single-user applications - Read-only data streaming (use Server-Sent Events) ## Quick Decision Tree ``` Need real-time collaboration? ├── Text editing? → Operational Transform (OT) ├── JSON data structures? → CRDTs ├── Cursor tracking only? → Simple WebSocket + presence ├── Offline-first? → CRDTs (better offline merge) └── No conflicts possible? → Basic broadcast ``` --- ## Technology Selection ### Conflict Resolution Strategies (2024) | Strategy | Best For | Complexity | Offline Support | |----------|----------|------------|-----------------| | Operational Transform (OT) | Text, ordered sequences | High | Limited | | CRDTs | JSON objects, sets | Medium | Excellent | | Last-Write-Wins | Simple state | Low | Basic | | Three-Way Merge | Git-style editing | High | Good | **Timeline**: - 2010: Google Wave uses OT - 2014: Figma adopts CRDTs - 2019: Yjs (CRDT library) released - 2022: Automerge 2.0 (CRDT library) released - 2024: PartyKit simplifies real-time infrastructure --- ## Common Anti-Patterns ### Anti-Pattern 1: Broadcasting Every Keystroke **Novice thinking**: "Send every change immediately for real-time feel" **Problem**: Network floods with tiny messages, poor performance. **Wrong approach**: ```typescript // ❌ Sends message on every keystroke function Editor() { const handleChange = (text: string) => { socket.emit('text-change', { text }); // Every keystroke! }; return