An event when documents have synced

I have a yjs client and a web socket provider with persistence. When a new client connects to a room, the client’s document merges with the version on the websocket server. I need to know (e.g. by observing an event) when the client version and the server version have completed merging and are now identical. The WSProvider event called ‘sync’ does not do this - it merely notes when the connection has been established.

I am currently using code such as

yMap.observe((evt) => { ... process the incoming changed key/values })

and

doc.transact(() => {yMap.set(... set key/map values...)})

as the interface to yjs. I am not using Y.encodeStateAsUpdate or Y.applyUpdate.

My documents can be up to 5 MB and so transmission and merging will not be instantaneous. I am surprised that I can’t find an event to do this in the documentation. Am I missing something obvious?

y-websocket should emit the sync event once the client receives all data from the server. If that doesn’t work, it would be a bug.

It could be, that your specific y-websocket implementation syncs before it receives the data from the database (in which case, your backend would sync an empty document). That is a known bug when using y-leveldb, for example.

An easy workaround is to set some kind of flag on the server once the data is fetched. E.g. if (ydoc.getMap().get('isSynced') == null) ydoc.getMap().set('isSynced', true) after it synced with the database. Then you can listen to changes on isSynced on the client.

Yes, I am using the standard y-websocket-serverand y-leveldb, so this might be the source of the issue. If it is a known problem, has anyone already got code to fix it?