Yes. A WebRTC data channel (RTCDataChannel) can carry text or binary data between two browsers, so a collaborative editor can use it to transmit edits. Suppose Lin and Chen are editing “周五开会” (meet on Friday). Lin adds “下午” (afternoon) after “周五”, while Chen inserts “短” (short) after “开”. If both messages arrive, will both people see “周五下午开短会” (have a short meeting on Friday afternoon)? That depends on what the editor sends and how it updates the document on receipt.1
Which edit gets lost if you send the contents of the text box?
We can reason through lecepin’s shared whiteboard example. It connects browsers to a server using WebSocket. When sending a message, it reads the entire text box value. The server forwards that message to others, who run textarea.value = message.content. If just one person types, everyone else’s text box soon shows the same text.2
Now suppose each person sends their version before receiving the other’s message:
| Time | Lin’s text box | Chen’s text box |
|---|---|---|
| Both open the same line | 周五开会 | 周五开会 |
| Each inserts characters locally and sends the full text | 周五下午开会 | 周五开短会 |
| Each receives the other’s full text and overwrites the box | 周五开短会 | 周五下午开会 |
The “下午” Lin just typed disappears from Lin’s screen, but it is still on Chen’s. Reliably sending the same two complete strings in order via WebRTC can produce exactly the same outcome. There was only one message in each direction, so nothing could even arrive out of order. The problem is replacing the recipient’s local edits with the sender’s entire text.
How do OT and CRDTs preserve both insertions?
Both writers begin with the same document. The vertical bar | marks where Chen wants to insert “短”:
小陈的原文 周五开|会 第 3 个字后
小林添字后 周五下午开|会 第 5 个字后
Operational transformation (OT) adjusts an incoming operation to account for concurrent edits already applied. Chen sends “insert ‘短’ after the third character.” On Lin’s copy, which already contains “下午”, this must become “insert ‘短’ after the fifth character” to stay between “开” and “会”. Transformation can happen on the client. The classic Jupiter system has a central server, yet clients and the server both transform incoming edits against operations they have already executed. The earlier Ellis and Gibbs algorithm also described a distributed approach.3
A conflict-free replicated data type (CRDT) gives replicas a common set of merge rules. For text, Yjs offers a shared type called Y.Text that can merge edits. An editor inserts characters locally with insert, and Yjs encodes the change as a binary update. Each insertion has its own identifier and remembers the identifiers of its left and right neighbors at insertion time. Even if “短” arrives late, it can find its original position between “开” and “会”.45
The Yjs documentation states the condition for agreement: “All clients will sync up when they received all document updates.” After receiving all updates, both sides get “周五下午开短会”. Even if both people insert at exactly the same position, Yjs gives their replicas the same ordering. The writers still have to judge whether it reads well.6
Do these updates still need a server over WebRTC?
The same Yjs document can be connected in two ways. With y-websocket, each browser connects to a central server, which distributes updates and can persist them. The replicas still merge using the Yjs CRDT rules. Looking only at whether a system has a central server does not tell you whether it uses OT or CRDTs.7
With WebRTC, two browsers first exchange the information needed to establish a connection. This step is called signaling. They typically find each other through a signaling server and then try to connect directly. When they cannot, a TURN server can relay the data. The way WebRTC finds a path also explains why WebRTC can sometimes see your broadband address even when a web page shows a proxy IP.1
For a Yjs editor in two browsers, each side passes its local Y.Doc (document replica) and the same room name to WebrtcProvider from y-webrtc. Once connected, Lin inserts “下午”, Yjs generates an update, and y-webrtc delivers it to Chen. Chen’s “短” travels in the opposite direction. The simplest test is to look at both screens: both edits should be present on each.86
Footnotes
-
Ristic, D., & Dutton, S. (2014-02-04). Send data between browsers with WebRTC data channels. web.dev. Signaling, STUN, and TURN for connection establishment and sending and receiving data. ↩ ↩2
-
lecepin. (n.d.). The Art of Real-Time Collaboration: Building an Efficient Shared Whiteboard. Juejin. See section 4 of the same article on GitHub, where 4.1 shows server forwarding and 4.2 shows the browser sending and receiving the whole text box value. ↩
-
Nichols, D. A., Curtis, P., Dixon, M., & Lamping, J. (1995). High-latency, low-bandwidth windowing in the Jupiter collaboration system. ACM UIST. Abstract on distributed predecessors and the centralized architecture, Figure 3 and the following transformation example. ↩
-
Yjs. (n.d.). Y.Text. Yjs Docs. Shared text and insertion. ↩
-
Yjs. (n.d.). Yjs Internals. GitHub. List items section on identifiers and neighbors. ↩
-
Yjs. (n.d.). Document Updates. Yjs Docs. Quotation in the opening paragraph and the Update API. ↩ ↩2
-
Yjs. (n.d.). y-websocket. Yjs Docs. WebSocket provider documentation on server distribution and persistence. ↩
-
Yjs. (n.d.). WebRTC connector for Yjs. GitHub. Client code and Signaling sections. ↩