Deleting local CRDT's state when entering new room using network provider

Hi everyone, so currently I’m developing a collaborative room-based graph-editor app using yjs and y-webrtc, and the app works like this (mostly like google docs):

  1. When first starting the app, a new empty room will be created for you with a room id.
  2. You can share your room id with your peers so they can join your room.
  3. User’s in this app can join other rooms from any room.
  4. When a user leaves a room, the user’s local CRDT state from last room will be deleted, and the CRDT state of the new room will be loaded as the new local state.

Now, I’m encountering a bug at step (4), that SOMETIMES when user A joins user B’s room, instead of user A seeing user B’s last state, the opposite happens (user B’ state will be overwritten by user A, which is a very very bad user experience). I’ve done some digging about this issue and found that this is the intended behaviour of CRDT’s sync mechanism.

Is there a way to tell the network provider to ignore (or delete) the local state when changing room id so instead of merging user A and user B’s state, it will just use user B’s state entirely? I’ve tried using ydoc.destroy() but the bug is still occuring.

Thanks in advance for your help!!

Welcome to the discussion board @abdashaffan,

What would happen when two users open a document at the same time and delete their local state once they sync with the other client?

Unless you implement some voting algorithm, there is no way to tell who should delete the content.

There is a relevant discussion to this problem here: Initial offline value of a shared document

Hope that helps. Cheers,
Kevin

Hi Kevin, first of all thanks for your response and for this great project,

It turns out that I’ve been using the ydoc.destroy() API incorrectly, so right now I’ve implemented a workaround like this:

when a user joins a new room from another room, first I’ll destroy the awareness listener, then i’ll create a new empty ydoc document (the awareness was already destroyed so all other users in the previous room will not be affected by the state deletion) so that when both states got synced, the state from the new room will always “wins”, since the new user joins the room with empty ydoc state.

is there a problem that will arise from this pattern? I’m still green to collaborative app development so I can’t yet foresee the use cases that will cause a problem.

also, I’ve read the discussion link, for my use case though, it’s impossible to have more than one user starts with a similar url in offline mode.

Regards,
Abda

So you are basically creating a new Y.Doc when you switch to a different room and then connect it to the a new URL.

Yeah, that sounds reasonable. You can also just destroy the old provider (provider.destroy() instead of destroying only the awareness instance).

But I feel that I was missing something from your explanation.