Webrtc - get value from db for first peer only

Hey YJS team, we are using webrtc and looking for a solution for the following:

get value from a db for first connected peer.
if peer is not first to connect then will not trigger get value from db


Hi @CoCreator-Frank,

I’m not sure how you would reliably detect the first client. You only get a message when you are connected to a remote client (the “synced” event).

I recommend that you store the Yjs document on a server. The Yjs document can always be merged. Every client can retrieve the initial content from the server and merge the document with the other clients.

Thanks for the reply. We have a use case where we are fetching a string from db to edit collaboratively using webrtc. At this point the value exist only in db until it is inserted in to a y.doc. Then yjs would work as designed from this point on. in the attempt to get the value for the first user and inserting, for each new peer it also gets the value and inserts. we were trying to see if there is an event when a peer connects to a peer and use this to exclude the get value from db function.

Yeah, there is the “synced” event and there is a dedicated event that fires when a specific peer is synced. Even better, you can listen to changes on the awareness instance.

But if the other peer has a bad connection, it can take quite some time until they connect (on 3g it may take more than 10 seconds). So there is no way to reliably detect if you are the first user.

It will be much easier to store the Yjs document alongside the string value in the db. Especially when using y-webrtc. You could easily write a script to create Yjs document from the script and put that in the database.

Interesting concept thank you. We could get yjs document by default and if one does not exist then get string. Thank you. I think this could work.