field-manual icon indicating copy to clipboard operation
field-manual copied to clipboard

Seems loading remote db is not working

Open ajayrao80 opened this issue 7 years ago • 2 comments

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.

ajayrao80 avatar Sep 17 '18 06:09 ajayrao80

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.

cristiano-belloni avatar Sep 20 '18 15:09 cristiano-belloni

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

aphelionz avatar Sep 27 '19 16:09 aphelionz