The Sync Protocol for OrbitDB synchronizes the database operations module:Log between multiple peers.
The Sync Protocol sends and receives heads between multiple peers, both when opening a database and when a database is updated, ie. new entries are appended to the log.
When Sync is started, a peer subscribes to a pubsub topic of the log's id. Upon subscribing to the topic, peers already connected to the topic receive the subscription message and "dial" the subscribing peer using a libp2p custom protocol. Once connected to the subscribing peer on a direct peer-to-peer connection, the dialing peer and the subscribing peer exchange the heads of the Log each peer currently has. Once completed, the peers have the same "local state".
Once the initial sync has completed, peers notify one another of updates to the log, ie. updates to the database, using the initially opened pubsub topic subscription. A peer with new heads broadcasts changes to other peers by publishing the updated heads to the pubsub topic. Peers subscribed to the same topic will then receive the update and will update their log's state, the heads, accordingly.
The Sync Protocol is eventually consistent. It guarantees that once all messages have been sent and received, peers will observe the same log state and values. The Sync Protocol does not guarantee the order in which messages are received or even that a message is recieved at all, nor any timing on when messages are received.
Examples
// Using defaults
const sync = await Sync({ ipfs, log, onSynced: (peerId, heads) => ... })
// Using all parameters
const sync = await Sync({ ipfs, log, events, onSynced: (peerId, heads) => ..., start: false })
sync.events.on('join', (peerId, heads) => ...)
sync.events.on('leave', (peerId) => ...)
sync.events.on('error', (err) => ...)
await sync.start()
Namespaces
Methods
(async) Sync(params) → {module:Sync~Sync}
Creates a Sync instance for sychronizing logs between multiple peers.
Parameters:
Name | Type | Description | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
params |
Object | One or more parameters for configuring Sync. Properties
|
Returns:
sync An instance of the Sync Protocol.
- Type
- module:Sync~Sync