mirror of
https://github.com/orbitdb/orbitdb.git
synced 2025-06-03 20:56:37 +00:00

* docs: Update README to match new version. * docs: Update events example to use new API. * docs: Correctly print out db query results. * test: Remove concurrent. * test: Remove unimplemented and 3rd party AC tests. * test: Remove unimplemented and 3rd party identity tests. * docs: Move jsdoc config to conf directory. * Point package.json main at index.js to access all exported functions. * docs: Vetted AC docs; these examples should work if implemented in code. Explicitly show orbit-db function imports. * docs: Fix incorrectly declared write objects. * docs: Improved canAppend documentation. Better JS syntax highlighting. * docs: wss and define filters for localhost separately. * docs: Simplified webSockets implementation with filters. * docs: Return manifest json only (no hash). JS highlighting. * docs: Remove operations documentation. * docs: Update heading levels. * docs: Differentiate between db types which expose put/add function. * docs: Correctly import IPFS and pass config. * docs: A simple method for full db replication. * docs: Link to existing examples of db implementation. * docs: Update heading. * docs: JS code formatting. import statements. * docs: Expand on the concepts of identities and identity management. * docs: Describe head sync-ing and full replication. * docs: Comprehensive explanation of setting up a db and sync-ing/replicating data across peers. Examples can be run in node.js. * docs: Syntax highlighting. Correct code implementation for custom/3rd party storage implementations. * docs: Getting started cleanup. * docs: Manifest as an IPLD data strcture.
114 lines
3.1 KiB
Markdown
114 lines
3.1 KiB
Markdown
# Replication
|
|
|
|
Below is a simple replication example. Both peers run within the same Node daemon.
|
|
|
|
```
|
|
import { OrbitDB } from 'orbit-db'
|
|
import { create } from 'ipfs-core'
|
|
|
|
// The config will set up a TCP connection when dialling other node.js peers.
|
|
// You can find out more about peer connectivity at https://connectivity.libp2p.io/.
|
|
const config1 = {
|
|
Addresses: {
|
|
API: '/ip4/127.0.0.1/tcp/0',
|
|
Swarm: ['/ip4/0.0.0.0/tcp/0'],
|
|
Gateway: '/ip4/0.0.0.0/tcp/0'
|
|
},
|
|
Bootstrap: [],
|
|
Discovery: {
|
|
MDNS: {
|
|
Enabled: true,
|
|
Interval: 0
|
|
},
|
|
webRTCStar: {
|
|
Enabled: false
|
|
}
|
|
}
|
|
}
|
|
|
|
const config2 = {
|
|
Addresses: {
|
|
API: '/ip4/127.0.0.1/tcp/0',
|
|
Swarm: ['/ip4/0.0.0.0/tcp/0'],
|
|
Gateway: '/ip4/0.0.0.0/tcp/0'
|
|
},
|
|
Bootstrap: [],
|
|
Discovery: {
|
|
MDNS: {
|
|
Enabled: true,
|
|
Interval: 0
|
|
},
|
|
webRTCStar: {
|
|
Enabled: false
|
|
}
|
|
}
|
|
}
|
|
|
|
const ipfs1 = await create({ config: config1, repo: './ipfs/1' })
|
|
const ipfs2 = await create({ config: config2, repo: './ipfs/2' })
|
|
|
|
// The decentralized nature if IPFS can make it slow for peers to find one
|
|
// another. You can speed up a connection between two peers by "dialling-in"
|
|
// to one peer from another.
|
|
// const ipfs1PeerId = await ipfs1.id()
|
|
// await ipfs2.swarm.connect(ipfs1PeerId.id)
|
|
|
|
const orbitdb1 = await OrbitDB({ ipfs: ipfs1, id: 'userA', directory: './orbitdb/1' })
|
|
const orbitdb2 = await OrbitDB({ ipfs: ipfs2, id: 'userB', directory: './orbitdb/2' })
|
|
|
|
// This opens a new db. Default db type will be 'events'.
|
|
const db1 = await orbitdb1.open('my-db')
|
|
|
|
// We connect to the first db using its address. This initiates a
|
|
// synchronization of the heads between db1 and db2.
|
|
const db2 = await orbitdb2.open(db1.address)
|
|
|
|
// We write some data to db1. This will not be replicated on db2 until we
|
|
// explicitly request these records using db2's iterator or all() convenience
|
|
// function.
|
|
await db1.add('hello world 1')
|
|
await db1.add('hello world 2')
|
|
await db1.add('hello world 3')
|
|
await db1.add('hello world 4')
|
|
|
|
let db2Updated = false
|
|
|
|
// Listen for the connection of ipfs1 to ipfs2.
|
|
// If we want to listen for connections from ipfs2 to ipfs1, add a "join"
|
|
// listener to db1.
|
|
db2.events.on('join', async (peerId, heads) => {
|
|
// The peerId of the ipfs1 node.
|
|
console.log(peerId, (await ipfs1.id()).id)
|
|
})
|
|
|
|
// Listen for any updates to db2. This is especially useful when listening for
|
|
// new heads that are available on db1.
|
|
// If we want to listen for new data on db2, add an "update" listener to db1.
|
|
db2.events.on('update', async (entry) => {
|
|
// Full replication is achieved by explicitly retrieving all records from db1.
|
|
console.log(await db2.all())
|
|
db2Updated = true
|
|
})
|
|
|
|
// wait for db2 to complete updating.
|
|
await new Promise((resolve, reject) => {
|
|
setInterval(() => {
|
|
if (db2Updated) {
|
|
resolve()
|
|
}
|
|
}, 1000)
|
|
})
|
|
|
|
// Close db1 and its underlying ipfs peer.
|
|
await db1.close()
|
|
await orbitdb1.stop()
|
|
await ipfs1.stop()
|
|
|
|
// Close db2 and its underlying ipfs peer.
|
|
await db2.close()
|
|
await orbitdb2.stop()
|
|
await ipfs2.stop()
|
|
```
|
|
|
|
Refer to the API for more information about [OrbitDB's synchronization protocol](https://orbitdb.org/api/module-Sync.html).
|