Example
This example highlights how to integrate automerge’s sync protocol with iroh’s peer-to-peer connectivity.How it works
First, we write a protocol wrapper for the Automerge sync protocol. This code exposes a small iroh protocol handler that synchronizes an Automerge Document between two peers over a bi-directional stream. It uses Automerge’s sync protocol messages, sent as length-prefixed binary frames, and drives a sync loop until both sides report “done.” Notifies the rest of the program when sync finishes by sending the final Automerge document over an mpsc channel. During sync, the dialer opens a bidirectional stream, repeatedly generates Automerge sync messages, sends them, receives the peer’s response, integrates any remote changes, and terminates when both sides report completion. The responder follows the same loop but waits for the connection to close, ensuring both replicas converge and the final document is sent through the sync_finished channel.initiate_syncruns the sync loop for the dialing side: it opens a bidirectional stream, repeatedly emits Automerge sync messages (length-prefixed) while consuming messages from the peer, merges remote changes into the shared doc, and exits once both sides have no more data, finally closing the connection.respond_syncmirrors the loop for the accepting side, waiting for the peer to close. The protocol handler’s accept entry point delegates to respond_sync, converts errors into AcceptError.- On successful completion, it sends a forked copy of the synchronized document through the provided channel so other components can observe the finished state.
send_msgandrecv_msgimplement the wire format for Automerge sync messages on top of iroh’s QUIC streams by prefixing each serialized message with its length. A zero-length frame indicates that the sender has no data to transmit in that round.