How can I tell if two documents are synced?

It seems that computing and comparing state vectors is not enough, since they don’t contain the delete set.

I see that a function called snapshotContainsUpdate exists, but it’s behavior is not documented. Should I compute updates from each documents (e.g. with encodeStateAsUpdate and use that function to check them against the other document’s snapshot?

I’m trying to implement this in the context of a remote provider, in which case the remote would send an ack to the client after the data has been persisted on the server side to let the client know. I’m trying to understand what data the remote would have to send back to the client so that it could perform the comparison.

1 Like

Wait, if the vectors don’t include the delete set, how can the calculated updates then result in synced documents?

I would expect that two documents are in sync if their state vectors are identical?

Two documents are in-sync if their snapshots are identical.

You could request a snapshot from the backend (a snapshot contains the state vector AND the delete set). Then, check for equality of the bytearray. If you apply the check synchronously, then you should get a positive result even if other users are currently typing (sorry, I think in our meeting I said something different).

However, this still doesn’t give you a method to recover. If the snapshots don’t match, then you are in serious trouble (unless local updates are currently en route). It would mean that you missed some updates from other users. This really shouldn’t happen. In this case, I suggest that you close the connection and force the provider to perform a full sync again.

So is it really enough to use state vectors to sync, as per the documentation?

And if during sync the state vectors are the same, that’s then no reason to skip steps right?

Yes, because the peer will always reply with the full delete set, which is quite small.

Never skip a sync. The recommended approach to sync is using state vectors, as the documentation describes. The reply (Y.encodeStateAsUpdate(ydoc, statevector)) should always be sent.

2 Likes