diff --git a/README.md b/README.md index 2e2b1833..96f87851 100644 --- a/README.md +++ b/README.md @@ -701,6 +701,31 @@ Y.applyUpdate(ydoc1, diff2) Y.applyUpdate(ydoc2, diff1) ``` +### Example: Syncing clients without loading the Y.Doc + +It is possible to sync clients and compute delta updates without loading the Yjs +document to memory. Yjs exposes an API to compute the differences directly on the +binary document updates. + + +```js +// encode the current state as a binary buffer +let currentState1 = Y.encodeStateAsUpdate(ydoc1) +let currentState2 = Y.encodeStateAsUpdate(ydoc2) +// now we can continue syncing clients using state vectors without using the Y.Doc +ydoc1.destroy() +ydoc2.destroy() + +const stateVector1 = Y.encodeStateVectorFromUpdate(currentState1) +const stateVector2 = Y.encodeStateVectorFromUpdate(currentState2) +const diff1 = Y.diffUpdate(currentState1, stateVector2) +const diff2 = Y.diffUpdate(currentState2, stateVector1) + +// sync clients +currentState1 = Y.mergeUpdates([currentState1, diff2]) +currentState1 = Y.mergeUpdates([currentState1, diff1]) +``` +
Y.applyUpdate(Y.Doc, update:Uint8Array, [transactionOrigin:any])
Y.encodeStateVector(Y.Doc):Uint8Array
Y.mergeUpdates(Array<Uint8Array>)
+ Y.encodeStateVectorFromUpdate(Uint8Array): Uint8Array
+ Y.diffUpdate(update: Uint8Array, stateVector: Uint8Array): Uint8Array
+ Y.encodeStateAsUpdate(ydoc, stateVector)
but works
+on updates instead.
+