automerge / automerge-classic

A JSON-like data structure (a CRDT) that can be modified concurrently by different users, and merged again automatically.
http://automerge.org/
MIT License
14.76k stars 467 forks source link

Added a simple example to help understanding the sync protocol #458

Open FelixSelter opened 2 years ago

FelixSelter commented 2 years ago

I added a simple example of two peers connecting and syncing a document.
This should make things more clear and avoid issues like this one #437
Previously you had to investigate the tests to understand this correctly

I hope this helps. Great work you all

ept commented 2 years ago

Hi @FelixSelter, thanks for writing this up. I think the code example is not quite correct though. If I read it correctly, you're taking a message from Automerge.generateSyncMessage() and broadcasting it to several connected peers. That's not safe, because a sync message is always specific to two particular peers. You need a separate syncState for each connected peer, and generate a separate sync message to send to each connected peer.

FelixSelter commented 2 years ago

I will correct that. Strange it actually worked for me with multiple peers

FelixSelter commented 2 years ago

@ept Can you please have a look at it again. It still seems wrong. 1 syncs with 2 2 applies the changes and sees that it does not need to sync back to 1

However if peer1 calls sync again without changing anything it will send out changes to peer2 which also applies them.

timfpark commented 2 years ago

Thanks for doing this @FelixSelter - this is super helpful to understand the protocol with implementation details.