How do I set an explicit priority for merge data from auth and anonymous users?

Good afternoon, colleagues. I need your help. I am trying to implement a data synchronization service in my browser extension. For example, to synchronize notes, tasks, or bookmarks. The task is complicated because the user can edit his data without the network. And besides, he can start using the product without authorization and then authorize it into the existing account. This is my main problem.

Let’s assume this case study. The user logged in and got a complete, up-to-date copy of his data from the server. Then he logs out of the account. Yes, he lost contact with the server. But the local data remains, and he can edit them. The user created one new note and deleted one existing one. Then he logged back into his account.

I want the data already on the server to completely overwrite the data that the user edited locally without an account (what was on the server under the password cannot be overwritten by an anonymous user). And the new local data was synchronized with the server. This is the default behavior in, e.g., Google Chrome’s synchronization.

But I don’t understand how I can achieve this with Yjs. I would be glad to get any ideas.

Hi @Yankovich,

For Yjs it doesn’t matter when and how you sync documents. Yjs simply syncs them. If you design your application carefully, you should be able to implement it with Yjs the way you want to.

It seems you want to have some custom sync rules depending on the authentication state. Working with Yjs document updates is fairly easy. There is a section about document updates on our documentation website:

I recommend that you start by reading the documentation and experiment with the different providers (y-websocket, y-indexeddb, …). Get some feeling of how Yjs syncs changes on the different shared data types. The best approach is to explore by experimenting. Have fun!

1 Like