Event that fires when Y.Doc is synced

Has y DOC got a property to know when it sent back the data for the first time ? I would like to show the editor when the data is already mounted to avoid the NO DATA miliseconds.

Hi @dmnogoud

Do I understand correctly that you want to know when the Yjs document is synced with the remote peers? I.e. when you propagated your data.

Hi @dmonad

Yes, I want to show the editor when the Yjs document is synced ( when the first update event happened ) to avoid show the editor without data for milliseconds before the update event.

What I did is create a local state to handle it with beforeTransaction event but I would like to know if there is a way to know the data is synced ( ready to show for the editor )

It’s pretty hard to determine when users are synced. I’m struggling with the definition mostly. If you have a central server the data might be “synced” when data is uploaded and pulled from the central server. Is the data “synced” when you load it from your local database (e.g. y-indexeddb)?

With y-webrtc, you might be synced when you have all the data from all users (which is often not possible).

Yjs is eventually consistent, which - in my opinion - means that you shouldn’t make any assumptions when data is synced. Your local copy is always up to date from your perspective.

Nevertheless, most providers implement a “synced” or “sync” event that is fired when data is synced with any other user / server. In the future, the db-adapters might fire a “load” event that is fired when data is loaded from the local database.

But note that the content might be available before “synced” is fired because all network providers also communicate over BroadcastChannels - pulling data from other browser tabs.

The answer you are looking for is probably just provider.on('synced', () => ..) - which will work in y-webrtc and y-websocket. My recommendation is not to use the synced event if possible, because the definition of “synced” is flawed unless you have a central server. Instead you could check if the content is empty (doc.getXmlElement().length > 0) or simply assume that syncing is pretty fast.

3 Likes

Thanks you @dmonad, now is clear how it works. It would be crear have this one in the db-adapters. I would for sure go for the xmlelement trick.

if I have a central server, how to implement it better?

1 Like