orbitdb / field-manual

The Offical User's Guide to OrbitDB
210 stars 44 forks source link

Does Orbit DB only sync after a write? #62

Open DanielVF opened 5 years ago

DanielVF commented 5 years ago

In the OrbitDB docs, I read: "In order to have the same data, ie. a query returns the same result for all peers, an OrbitDB database must be replicated between the peers. This happens automatically in OrbitDB in a way that a peer only needs to open an OrbitDB from an address and it'll start replicating the database."

However, the behavior I'm seeing while testing this locally is that when I merely open a DB, no syncing happens.

I have one IPFS server, one Orbit Database, two local instances of that database, each one with some heads that the other does not have has. I start up both instances, and no message are sent on IPFS PubSub, and both DB's remain unsynced.

If I do a write at the point that both are online, they both pickup that new head, and follow the next's back from that write. But merely opening a database connecting does not sync.

Is this intended behavior? Could this be due to my local DB's instances using the same user public key? And/or is this because I'm sharing a remote IPFS server?

Source below:

const IpfsApi = require('ipfs-api')
const OrbitDB = require('orbit-db')

const ipfs = IpfsApi('localhost', '5001')
const orbitdb = new OrbitDB(ipfs)

async function run(){
    const db = await orbitdb.open('/orbitdb/MYDBHASHHERE/test-db')
    await db.load()

    const result = db.iterator({ limit: -1 }).collect()
    console.log("Start")
    console.log(JSON.stringify(result, null, 2))

    db.events.on('replicated', (address) => {
        console.log("REPLICATED")
        console.log(db.iterator({ limit: -1 }).collect())
    })
}

run()
DanielVF commented 5 years ago

I think OrbitDB depends on seeing a new IPFS peer address to start a head exchange. If the only OrbitDBs connected to a database are on the same IPFS peer, then OrbitDB doesn't see another IPFS peer connected to that database, doesn't start a head exchange, and both DB's stay unsynced until a write happens.

aphelionz commented 5 years ago

Moving this to the Field Manual repo so we can discuss in more depth