YousefED / SyncedStore

SyncedStore CRDT is an easy-to-use library for building live, collaborative applications that sync automatically.
https://syncedstore.org
MIT License
1.71k stars 51 forks source link

Document Updates for Persistence #37

Closed vincent911001 closed 2 years ago

vincent911001 commented 2 years ago

Hi Yousef,

Good day,

Is it possible for you to give me some pointers around document updates in SyncedStore for persistence?

Currently, I am thinking of computing the the diff of YDoc by getting the previous state before editing and current state after editing. The diff will then be persisted to database for future retrieval.

    const doc = getYjsValue(store);
    const prevState = Y.encodeStateAsUpdate(doc);

    // editing happens here

    const newState = Y.encodeStateAsUpdate(doc);
    const diff = Y.diffUpdate(prevState, newState);
        // the diff will then be persisted to database

However, the diff have to happen in every functions that are going to be used for editing.

The second option is to listen for the update event on YDoc:

doc.on("update", (update, origin) => {
  console.log("update from doc: ", update, origin);
  // however, origin is null thus couldnt differentiate whether the updates happens locally or from remote peers
});

FYI, the connection provider is WebRTCProvider. Thus, if I am able to determine origin, I could persist the updates to database if the updates happens locally. Is there a way to get to know the origin to differentiate oneself from other peers?

The document updates would then be retrieved from database and merged locally in each of the clients if needed.

Can I know whether my understanding and ideas is correct?

Thanks, Vincent

YousefED commented 2 years ago

Hi and welcome!

The way to go is indeed to register an update listener, and persist those updates to a database (perhaps batched, depending on how often updates happen). This thread can help you to determine whether an update was local or not: https://discuss.yjs.dev/t/determining-whether-a-transaction-is-local/361.

However, I'd recommend registering a sync provider that's specialized in persisting updates to a backend. Examples of these are Hocuspocus, y-redis , or y-websocket.