peer-sync

command
v1.61.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 3, 2026 License: MIT Imports: 10 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

peer-sync

Documents that converge with no server and no network: three peers exchange y-protocols messages over Go channels.

go run ./examples/peer-sync
offline: A="Hello from A. " B="Hello from B. "
B sends a step 1 with its state vector (6 bytes), A answers with a step 2 holding what it lacks (31 bytes)
A sends a step 1 with its state vector (6 bytes), B answers with a step 2 holding what it lacks (31 bytes)
synced: "Hello from A. Hello from B. "
live: "Hello from A. Hello from B. A again. B again. "
with C: "Hello from A. Hello from B. A again. B again. C joins. "
after B is back: "Hello from A. Hello from B. A again. B again. C joins. A meanwhile. B offline. "

What it shows:

  1. Offline edits. Each peer edits its own document.
  2. Catch-up in two steps each way. A peer sends a step 1 with its state vector (protocol.StartSync), the other answers with a step 2 holding the updates it lacks (protocol.HandleSync), and the first applies it (protocol.HandleSync again). These are y-protocols' sync messages in y-websocket's envelope (protocol.EncodeSyncMessage, protocol.Read), the bytes y-websocket and Hocuspocus clients send.
  3. Live updates. Doc.OnUpdate hands over every committed transaction as an update, sent to the linked peers as an update message (protocol.EncodeUpdate).
  4. A late peer joins: linked to every peer, then synced both ways with each of them (join).
  5. A peer cut off keeps editing and comes back the same way.

Things to copy right:

  • OnUpdate runs with the document's write lock held. Only stage the bytes there (an unbounded outbox here); a forwarder goroutine sends them and an applier goroutine applies what arrives, so a slow or full peer slows a goroutine, never a document.
  • A step 1 is answered by the peer that received it, to the peer it came from: each message in the inbox carries its sender, and the reply goes out through the receiver's own sender goroutine, like its updates.
  • Link a peer for live updates before it catches up: an edit committed while it catches up then reaches it either way (an update that arrives before what it builds on waits as pending).
  • Catch a joining peer up from every peer it links to, not from one. A peer does not forward what it received, so an update whose sender had not yet linked the newcomer reaches it only through a sync with that sender.
  • Stop in order: stop every forwarder and wait for it, then close the inboxes and wait for the appliers. A forwarder still running could send on a closed inbox, and that panics.
  • Apply incoming updates with an origin of your own (remote{} here) and skip that origin in OnUpdate, or peers echo each other's updates forever.
  • Read a shared type inside a read transaction (Doc.ReadTxn), since other goroutines write to the document.
  • Updates are idempotent and commutative: applying one twice, or out of order, is safe (an update whose dependencies have not arrived waits as pending).

The messages are package protocol's, so the same code speaks to a y-protocols peer in JavaScript over any transport that carries messages; over a byte stream, frame them with protocol.WriteRecord and protocol.ReadRecord.

Documentation

Overview

Command peer-sync shows how ygo documents converge with no server and no network: peers exchange y-protocols messages over Go channels.

go run ./examples/peer-sync

Two peers edit offline, then sync in two steps each way (a step 1 with a state vector out, a step 2 with the updates it lacks back), stay in sync by forwarding every committed update, take a late third peer, and catch up after one of them edits while cut off. The messages are the ones y-websocket and Hocuspocus clients send (package protocol), so any transport that carries messages works the same way: a WebRTC data channel, a message queue, a socket with protocol.WriteRecord.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL