Implementing Operational Transform (OT) for Real-time Collaboration in Mobile Applications
Operational Transform — algorithm powering Google Docs, Notion, Etherpad. Not abstraction, but concrete math: each document change — operation (insert(pos, text) or delete(pos, len)), which transforms relative to concurrent operations so all clients converge to one result.
Understanding OT important not only for from-scratch implementation but for conscious choice between OT and CRDT.
The Problem OT Solves
Two users edit document "hello":
- User A:
insert(5, " world")→ "hello world" - User B simultaneously:
delete(0, 5)→ ""
Simply applying both operations in any order — results diverge. OT transforms A's operation accounting for B's:
-
transform(insert(5, " world"), delete(0, 5))→insert(0, " world")(position shifts because 5 chars deleted before it)
Result: " world" — identical on both clients.
Server Architecture for OT
OT requires server-coordinator. Scheme:
Client A ──→ Server ──→ Client B
↑ │ │
└─────────────┘ │
ACK + revision │
↓
Client B transforms
its pending operations
Server maintains operation history with revision numbers. Client sends operation with base revision number (operation created from). Server transforms incoming operation relative to operations applied after that revision, applies, returns confirmation and broadcasts transformed operation to all.
Client stores:
-
revision— last confirmed revision from server -
pending— sent but not confirmed operation -
buffer— operations entered whilependingnot confirmed
On receiving server operation: if client has pending, mutually transform server operation and client via transform(client, server) and transform(server, client).
Transform Algorithm for Text
For text operations transformation — position adjustment:
transform(insert(p1, s1), insert(p2, s2)):
if p1 < p2: return insert(p1, s1) // position unchanged
if p1 > p2: return insert(p1 + len(s2), s1) // shift right
if p1 == p2: return insert(p1, s1) // tie-breaking by userId
transform(insert(p1, s1), delete(p2, len2)):
if p1 <= p2: return insert(p1, s1)
if p1 >= p2 + len2: return insert(p1 - len2, s1)
else: return insert(p2, s1) // insert was inside deleted range
For formatting (rich text) transformation more complex — operations on attributes have own semantics.
Libraries: ot.js, sharedb
ot.js — pure JavaScript OT engine for simple text type. Works in React Native without modifications. Implements compose and transform operations. Doesn't include transport.
ShareDB — full framework: OT engine + WebSocket server + client. Supports pluggable operation types (json0, rich-text, custom). json0 allows OT on JSON documents — useful for structured data (not just text).
ShareDB client for React Native:
import ReconnectingWebSocket from 'reconnecting-websocket';
import ShareDB from 'sharedb/lib/client';
const socket = new ReconnectingWebSocket('wss://server.com/sharedb');
const connection = new ShareDB.Connection(socket);
const doc = connection.get('documents', documentId);
doc.subscribe(() => {
doc.on('op', (op, source) => {
if (!source) {
// remote operation — update UI
applyOpToEditor(op);
}
});
});
ReconnectingWebSocket — critical for mobile: on network change (Wi-Fi → 4G) auto-reconnects and restores sync.
Composition: Multiple Operations Into One
Fast typing generates dozens of operations per second. Batching: ot.js supports compose(op1, op2) — merge consecutive operations. Send compose-operation every 50–100ms instead of each separate.
Condition for compose: operations must be sequential (op2 applies after op1). If server operation arrives between op1 and op2 — compose impossible, transform separately.
OT vs CRDT: What to Choose By
| Criterion | OT | CRDT |
|---|---|---|
| Offline mode | Limited | Native |
| Server | Mandatory | Optional |
| Client complexity | Medium | Higher |
| Server complexity | Higher | Lower |
| Library maturity | ShareDB — production-ready | Y.js — production-ready |
| Rich text support | rich-text OT type | Y.Text with attributes |
Choose OT when: strict operation history needed, server-coordinator already exists, offline not required. CRDT — when offline mode important and P2P sync.
Assessment
ShareDB integration for text editor on React Native — 6–10 weeks (including server part). If need JSON-document OT (structured data) — 10–16 weeks. Implementing OT from scratch without ShareDB — not recommended: transform algorithm has edge cases hard to test.







