yjs / y-websocket

Websocket Connector for Yjs
https://docs.yjs.dev/ecosystem/connection-provider/y-websocket
MIT License
492 stars 255 forks source link

Server-side reloaded ydoc version is getting overwritten by client-side version upon page refresh #161

Open nvanhoeck-dpgmedia opened 9 months ago

nvanhoeck-dpgmedia commented 9 months ago

Hi there!

My team at DPG-Media have a question about the way we need to deal with something we encountered implementing the YJS-websocket-provider in our codebase. We have a frontend setup which uses SyncedStore and the yjs Websocket-provider to setup a communication between clients and the websocket-server. The communication goes pretty well, aside from some quirks.

The use case is that we want server updates, coming from a database change, to be sent to the websocket-server and cause a complete replacement of the Ydoc where the clients currently are working on in the browser. We have implemented a locking and warning mechanism, so that the document is temporarily locked for editing by manual edits.

We have discovered that something goes wrong with reloading the ydoc into the front-end upon refreshing the page. When only a single client has its browser open and he refreshes the page after the yDoc has been replaced with a newer version, everything goes well and we see a new version being reloaded. But when multiple clients have their browser open and one of them refreshes, we see that the clients 'version' gets loaded back again after the refresh, not the one coming from the database.

We have a difficult time to wrap our head around this one, since it is pure in the chronological way the websockets messages are sent. When tracking it down in our code, we see that the change from the database get applied first, but upon refresh, the version the clients gets applied afterwards.

Do you have any suggestions or examples how to deal with server-side updates to an Y-Doc that is being used? What causes that the version opened on multiple browsers gets reloaded on another clients page after a refresh, even when there is an update by the websocket-server replacing the y.doc with the new content?

If more info is required, please let me know and I'll see to it I can get it provided to you.

Thanks in advance.