Triamorph Systems

Full-Stack · 13 min read · Sep 08, 2024

Real-Time Collaborative Canvas and Document Architecture: Integrating React, TipTap, and Yjs CRDTs over WebSockets

Step-by-step architectural guide to building Google Docs-style real-time collaborative editors with React, TipTap, Yjs CRDTs, Hocuspocus WebSocket servers, and Redis clustering.

By Hammad Haider

Core Engineering Takeaways

  • CRDTs (such as Yjs) resolve concurrent character insertions and deletions locally without requiring a central server to arbitrate lock order, making offline editing friction-free.
  • Use the Yjs Awareness protocol to broadcast ephemeral presence data (multiplayer cursor positions, selections, and user avatar badges) without cluttering document storage.
  • Deploy Hocuspocus or custom Bun WebSocket servers backed by Redis Pub/Sub to scale real-time collaboration across multiple load-balanced container instances.

Building multiplayer collaborative applications (like Notion, Figma, or Google Docs) is one of the pinnacle challenges of full-stack engineering. Traditional database locking mechanisms fail because users cannot wait for server round-trips while typing at 80 words per minute. By pairing Yjs Conflict-Free Replicated Data Types (CRDTs) with TipTap (ProseMirror) and distributed WebSocket servers, documents synchronize peer-to-peer and across server clusters with mathematically guaranteed convergence.

1. Operational Transformation (OT) vs CRDTs

Why modern collaborative systems have migrated from central OT servers to CRDTs

Operational Transformation (OT), famously used in early Google Docs, requires a central server to sequence and transform concurrent editing operations. If the client loses connection, reconciling divergence is mathematically complex.

Conflict-Free Replicated Data Types (CRDTs) structure document characters into unique Lamport timestamps. Whether operations arrive in order or out of order, applying commutative operations guarantees identical document states across all clients.

Sync Latency
< 25ms
Binary WebSocket message transport
Document Convergence
100%
Mathematically guaranteed CRDT state
Concurrent Users
500+ / doc
Efficient binary state vector diffs

2. Connecting TipTap & React with Yjs Document State

Integrating ProseMirror transactions with Y.Doc shared types

TipTap provides a headless ProseMirror interface. Through the `y-prosemirror` binding, every keystroke mutates a `Y.XmlFragment`, broadcasting delta updates via WebSocket.

src/components/editor/CollaborativeEditor.tsx (typescript)
import React, { useEffect } from 'react';
import { useEditor, EditorContent } from '@tiptap/react';
import StarterKit from '@tiptap/starter-kit';
import Collaboration from '@tiptap/extension-collaboration';
import CollaborationCursor from '@tiptap/extension-collaboration-cursor';
import * as Y from 'yjs';
import { HocuspocusProvider } from '@hocuspocus/provider';

interface EditorProps {
  documentId: string;
  currentUser: { name: string; color: string };
}

export function CollaborativeEditor({ documentId, currentUser }: EditorProps) {
  const ydoc = React.useMemo(() => new Y.Doc(), [documentId]);

  const provider = React.useMemo(() => {
    return new HocuspocusProvider({
      url: process.env.NEXT_PUBLIC_COLLAB_WS_URL || 'wss://collab.triamorphsystems.com',
      name: documentId,
      document: ydoc,
    });
  }, [documentId, ydoc]);

  const editor = useEditor({
    extensions: [
      StarterKit.configure({ history: false }), // CRDT manages history locally
      Collaboration.configure({ document: ydoc }),
      CollaborationCursor.configure({
        provider,
        user: { name: currentUser.name, color: currentUser.color },
      }),
    ],
  });

  return (
    <div className="border border-border rounded-xl bg-surface p-6">
      <EditorContent editor={editor} className="prose max-w-none focus:outline-none" />
    </div>
  );
}

3. Real-Time Multiplayer Cursors & Selection Awareness

Broadcasting mouse positions and text highlights over ephemeral protocols

The Awareness protocol sends lightweight JSON payloads containing user names, colors, and cursor anchor indices. When a remote collaborator selects a paragraph, the UI renders their cursor name tag in real-time with CSS transitions.

4. Scaling Distributed WebSocket Servers with Redis Pub/Sub

Clustering real-time collaboration servers across Kubernetes pods

To scale beyond a single Node.js instance, Hocuspocus WebSocket servers are clustered via Redis Pub/Sub extension. When Node A receives a keystroke from Client 1, it broadcasts the binary update to Redis, propagating changes to Client 2 connected to Node B.

Tags: real time collaborative editor architectureyjs crdt react tiptap websocketshow to build google docs clone yjsmultiplayer cursor websocket server nodejsconflict free replicated data types frontend

Related Engineering Practice & Case Studies

Engineering Practice Blueprint

Custom Web Development & SaaS Platforms →

Next.js & React web platforms engineered for sub-1.5s load times and 98+ Lighthouse scores.

View Practice Blueprint & Capabilities →
Production Client System

Portfolix Developer Portfolio Builder →

Modern developer portfolio builder platform delivering sub-5 minute deployments with 98+ Lighthouse scores and 100% clean code export.

Read Deployed Case Study (Sub-5 minute edge deployment with 100% clean code export) →

Related Engineering Articles & Guides

Explore all technical guides in our Articles & Guides Index → or discuss your architecture with our engineers via Technical Discovery →