Skip to content

Collaborative text

Three independent replicas of one paragraph, side by side. Type into any of them and watch it appear in the others; take one offline, edit all three, bring it back, and watch a genuine divergence be reconciled without anybody losing a word.

Three replicas, one document

Below are three independent replicas of the same document, demo-text. Nothing is shared between them except the messages they send each other.

Each panel is one participant. Its title bar carries the replica's presence colour, its connection status (synced, syncing, reconnecting, offline…) and a button that disconnects it for real. Also here lists the other participants this replica can currently see, through BcrdtPresenceView. The footer counts the characters and the engine's storage blocks, and ends with the replica's client identifier - a different one in every panel.

Opening three replicas…

The thing worth trying. Take Replica A offline with its Go offline button. Type a sentence into it. Now type different sentences into B and into C - note that those two still see each other, because only A left. Then bring A back online. All three converge on a document containing every sentence, in the same order in all three panels. Nothing was locked, nothing was queued keystroke by keystroke, and nothing was overwritten. Without a CRDT, the usual outcome here is that whichever replica saved last wins and the other two sentences are gone.

What a keystroke becomes

Each panel has its own client identifier, its own presence identity, its own local storage and its own connection to the in-memory network the other two are on, which is why what you see is a real demonstration rather than three views of one variable.

Type into any panel. The edit is turned into the minimal insertion or deletion you actually made - not "the textarea now says this", which would replace every character and destroy concurrent work - encoded as a delta of a few bytes, sent, decoded, merged, and rendered in the other two. The coloured bar of each panel is that replica's presence colour, and it is the same colour its collaborators show next to its name in their own presence lists.

Each replica also keeps its document in a store of its own, standing for that participant's device. Leave this page and come back: every panel opens from what its store kept and reconnects, so the text is where you left it - including whatever a replica typed while it was offline and never got to send.

What the editor does for you

Every panel is the same one line of markup. BcrdtTextArea binds a <textarea> to a BcrdtText, and does the parts that are easy to get wrong:

  • It recovers the real edit from the control's new value, so a keystroke is a one-character insertion however long the text is, and concurrent edits elsewhere in the text are untouched.
  • It keeps your caret on the character it was on while somebody types above it.
  • It never lets a remote change land on top of a keystroke still on its way - an input method's composition included - and reads a paste too big for one message back in chunks.
  • It publishes your caret and selection through presence, anchored to content rather than to an index.
  • It turns read-only when its ReadOnly parameter says so, or when the server granted only read access, and reports through OnError if its small browser module cannot load.
<BcrdtTextArea Text="@document.Text("body")"
               Presence="document.Presence"
               Field="body"
               Rows="10"
               Placeholder="Type here" />

For an input you render yourself, BcrdtTextSynchronizer.Apply(text, newValue) does the first of those, and BcrdtTextInputBinding all of them without a component.

An unhandled error has occurred. Reload 🗙