← Back to Blog⚙️ Engineering · 7 min read

How Real-Time Sync Works Under the Hood

A technical deep-dive into the CRDTs, Yjs, Firebase, and WebSocket stack that makes ShareCode's collaboration feel instant.

Ever wondered how two people can type in the same document without overwriting each other? Here is a detailed look at the technology behind ShareCode's collaboration engine.

⚠️The Problem: Concurrent Edits

When two users type at the same position at the same time, a naive system would either lose one edit or create a conflict. Traditional Operational Transformation (OT) solves this with a central server that sequences operations — but OT is complex, fragile, and difficult to scale.

ShareCode takes a different approach using CRDTs (Conflict-Free Replicated Data Types), which guarantee convergence without any central arbitration.

🧬CRDTs and Yjs

ShareCode uses Yjs, a high-performance CRDT library written in JavaScript. Every character you type is assigned a unique, sortable ID based on a logical clock. When two users type at the same position, Yjs deterministically resolves the order — no server round-trip needed.

The key insight: CRDTs are mathematically guaranteed to converge. As long as all edits eventually reach all participants, every copy will be identical — regardless of arrival order. This is called strong eventual consistency.

🔥Firebase as the Persistence Layer

Each code space is backed by a Firestore document. The Yjs state is periodically snapshotted and saved, so even if everyone disconnects, the document is fully recoverable. Firebase Auth ensures only the owner can lock or delete a document.

This architecture means ShareCode does not need a custom backend server for storage. Firebase handles persistence, authentication, and security — letting the team focus on the editing experience.

Low-Latency Propagation

Edits travel from your browser to other participants via WebSocket-style connections managed by Yjs providers. In most scenarios, the round-trip is under 100 milliseconds — fast enough to feel instant. The protocol is binary-encoded and highly compressed.

Yjs also supports offline editing. If a participant loses their connection, they continue typing locally. When the connection resumes, Yjs merges the offline edits automatically — no data loss, no conflicts.

👁️Cursor Awareness

Beyond text synchronization, ShareCode syncs cursor positions and selections across users. You can see exactly where your collaborator is editing, turning the shared editor into something closer to a Google Docs experience for code.

Cursor awareness is implemented through the Yjs awareness protocol, which piggybacks on the same connection. It adds negligible overhead but dramatically improves the experience.

Why This Architecture Works

The combination of CRDTs + Firebase gives ShareCode two important properties: resilience (offline edits merge without conflicts) and simplicity (no self-hosted infrastructure required). The entire system runs in the browser with Firebase as the only backend dependency.

This means ShareCode can scale to thousands of concurrent code spaces without complex server orchestration. The CRDT model ensures correctness at the mathematical level — not just the implementation level.

Experience real-time sync yourself

Open a code space and invite a collaborator. See the magic in action. Free forever.

Try ShareCode Free →