First synchronization upon entering a room

Hi everyone, I’m having trouble understanding how to handle the following use case, I hope to find some help here:

1 - client A creates a yMap ‘map’ and sets the value ‘a’ on it
2 - client B creates the same yMap ‘map’ as client A and sets the value ‘b’ on it
3 - client A connects to a room via the webrtc provider
4 - client B connects to the same room via the webrtc provider

Which value should the yMap ‘map’ have after this four steps? I’ve done some tests but i cannot figure it out, some times is ‘a’ sometimes is ‘b’ for no apparent reason.

I there a way to handle this? Or at least make this predictable, let’s say always ‘a’ or always ‘b’.

Thank you very much.

Welcome @b1n01 to the discussion board,

This is the nature of the CRDT algorithm. From the perspective of Yjs, you are creating concurrent operations. Client1 sets value to a and client2 sets value to b. Yjs only guarantees that users end up with the same content.

May I ask, what is the specific use case for concurrently setting a value when the application starts?

Ok I see, I assumed this was the way the algorithm was, but I was hoping for some options to change the behavior while entering a room, for example, “create” if I assume the user is the first to enter the room and “join” if I assume some other has already created the room and set a value on the doc.

My use case is the following: different users can collaboratively select a file to render on the page from a list of files, when a user selects a file all other users will see that file. All users are in an “offline” mode when they load the page and only when they press a dedicated button they start synching whit other users. I was trying to do something like: if the room already exists use the file someone else selected, otherwise set your file as the selected file.

I recognize that it is not the algorithm’s fault, I can easily change some of my logic to make it work as expected. I was stubborn about the “create/join” logic because I’m using Sveltejs stores to wrap Yjs map and it was easier to always have a default value even if the user is offline.

Thank you very much for the reply, keep up the good work.