Skip to main content

WebSocket Document Operations

SDD Classification: L3-Technical Authority: Engineering Team Review Cycle: Quarterly
This guide covers document operations in real-time collaboration, including operational transform, conflict resolution, and best practices for implementing collaborative editing.

Operational Transform Overview

How OT Works

  1. Local First: Operations apply locally immediately for responsiveness
  2. Server Transform: Server transforms operations against concurrent changes
  3. Broadcast: Transformed operations sent to all other clients
  4. Client Transform: Clients transform incoming operations against pending local ops

Operation Types

Insert Operation

Add content at a position:

Delete Operation

Remove content from a range:

Retain Operation

Keep content unchanged (used in compound operations):

Format Operation

Apply formatting to a range:

Sending Operations

Operation Message Format

Client Implementation


Operational Transform Algorithm

Transform Rules

Insert vs Insert

Insert vs Delete

Delete vs Insert

Delete vs Delete


Conflict Resolution

Version Conflict

When operations are based on outdated versions:

Handling Conflicts


Rich Text Operations

Supported Attributes

Applying Formatting


Undo/Redo Support

Operation Inversion


Performance Optimization

Operation Batching

Combine rapid operations for efficiency:

Debouncing Cursor Updates


Best Practices

Do

  1. Apply operations locally first - Immediate feedback improves UX
  2. Use base version - Always include version for conflict detection
  3. Batch rapid operations - Reduce network overhead
  4. Store operation history - Enable undo/redo functionality
  5. Handle offline gracefully - Queue operations for later sync

Don’t

  1. Don’t skip transformation - Always transform remote operations
  2. Don’t ignore acknowledgments - They confirm server state
  3. Don’t send empty operations - Filter no-ops before sending
  4. Don’t assume operation order - Network may reorder messages
  5. Don’t lose operations - Persist pending ops locally


Document Status: Complete Version: 2.0