Skip to content

How It Works

Jethro Kuan edited this page Mar 29, 2017 · 2 revisions

How it works

ShareDB is used as the layer for real-time synchronization of text. This is achieved through operational transformation. ShareDB provides adapters for several databases, including MongoDB and postgresql.

Local changes are converted into shareDB ops, and are then sent to the server. These ops are transformed, and propogated to to client and then applied onto Ace text with deltas.

For example, inserting text where | is the cursor:

abc|
abcd|

Produces the following delta and its corresponding op:

“`

//Delta
{'start':{'row':1,'column':3},'end':{'row':1,'column':4},'action':'insert','lines':['d']}
// Op
op: [{"p":["code",3],"si":"d"}]

Here is what happens when `Ace1` makes a local change:

  1. onLocalChange listener on Ace1 triggered.
  2. delta transformed to shareDB op, and is sent to server
  3. server accepts op, applies OT.
  4. Transformed op is propogated to all listening ace instances: Ace2 and Ace1
  5. Ace2 receives shareDB op from server, triggers onRemoteChange.
  6. shareDB op transformed to ace delta, applied to editor.
  7. Application of delta triggers onLocalChange on Ace2, but source is self, and the operation is suppressed. This prevents a feedback loop.
Clone this wiki locally