The challenge: concurrent edits
Two users typing in the same document at the same time will conflict. Naive "last-write-wins" loses data. The two production-grade approaches are Operational Transforms (OT) and Conflict-free Replicated Data Types (CRDTs).
OT vs CRDT in one paragraph
OT (Google Docs) requires a central server. Each edit is an operation with an integer revision; the server transforms incoming operations against any concurrent ones to produce a consistent result. CRDT (Figma, Notion) requires no central authority — each character has a globally unique ID and ordering metadata, and the data structure mathematically converges no matter the operation order. CRDTs are easier to reason about; OT is more bandwidth-efficient.
Use a library
Implementing either correctly is months of work and the wrong place to be original. Yjs (CRDT) is the dominant JS library; Automerge is its main competitor. Both have WebSocket transport adapters. Wire them in; spend your time on UX, not algorithms.