orbitdb / field-manual

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

Seems loading remote db is not working #71

Open ajayrao80 opened 6 years ago

ajayrao80 commented 6 years ago
const IPFS = require('ipfs')
const OrbitDB = require('orbit-db')

const ipfsOptions = {
    start: true,
    EXPERIMENTAL: {
      pubsub: true,
    },
    config: {
      Addresses: {
        Swarm: [
          '/dns4/ws-star.discovery.libp2p.io/tcp/443/wss/p2p-websocket-star'
        ]
      },
    }
  }

  const ipfs = new IPFS(ipfsOptions)

  ipfs.on('error', (e) => console.error(e))

  ipfs.on('ready', async () => {
    const orbitdb = new OrbitDB(ipfs)

    const db = await orbitdb.open('ajdb', {
        create: true, 
        overwrite: true,
        localOnly: false,
        type: 'keyvalue',
        write: ['*'],
      })

    await db.load()

    await db.set('name', 'hello')

    const value = db.get('name')
    console.log(value)
 })

I'm testing this on two computers. Above code is in one computer.

Below code is in another. One computer creates the db and adds an entry to it another one tries to retrieve it.

const IPFS = require('ipfs')
  const OrbitDB = require('orbit-db')

  const ipfsOptions = {
    start: true,
    EXPERIMENTAL: {
      pubsub: true,
    },
    config: {
      Addresses: {
        Swarm: [
          '/dns4/ws-star.discovery.libp2p.io/tcp/443/wss/p2p-websocket-star',
        ]
      },
    }
  }

  const ipfs = new IPFS(ipfsOptions)

  ipfs.on('error', (e) => console.error(e))

  ipfs.on('ready', async () => {
    const orbitdb = new OrbitDB(ipfs)

    const db = await orbitdb.open('/orbitdb/QmcYbhRpK8pySXwpH5iTPTNna5aWBf2dE75UoLvC7ENFM5/ajdb', { sync: true })
    await db.load()

    const value = db.get('name')
    console.log(value)
  })

But second computer which is retrieving the entries (at least trying to) returns undefined. Not sure why i can't load a remote database. I've also tried querying the database after the replication event but replication event is not triggering at all.

cristiano-belloni commented 6 years ago

You should listen for the replicated event and try to query the database then. Please note that replicated is different from the replicate event, which instead triggers before replicating.

aphelionz commented 5 years ago

Moving this to the Field Manual so we can go into more detail