Dshiz
Dshiz

Reputation: 3331

Unable to retrieve data from IPFS by hash in another browser

I am experimenting with js-ipfs. I want to put some data on the network in browser A, then retrieve that data in browser B.

Using this example, I am successfully able to retrieve 'Hello world!' in both browsers, but when I change the data and try to cat the hash for that data I can only retrieve it in browser A. Browser B returns a promise that never resolves.

Works on both browsers:

QmQzCQn4puG4qu8PVysxZmscmQ5vT1ZXpqo7f58Uh9QfyY is the hash of the stored data: 'Hello world!'.

    async function catFile () {
      for await (const data of node.cat('QmQzCQn4puG4qu8PVysxZmscmQ5vT1ZXpqo7f58Uh9QfyY')) {
        console.log(data.toString())
      }
    }

    catFile()
> Promise {status: "pending"}
> Hello world!

Works only in browser A:

QmS15SwTbB7hKbEyvNATQcnj7cBPC37cCSobEoP8zssBYs is the hash of the stored data: 'Hello worldy world!'.

async function catFile () {
      for await (const data of node.cat('QmS15SwTbB7hKbEyvNATQcnj7cBPC37cCSobEoP8zssBYs')) {
        console.log(data.toString())
      }
    }

    catFile()
> Promise {<pending>} // Never resolves in browser B

Upvotes: 1

Views: 543

Answers (1)

Dshiz
Dshiz

Reputation: 3331

I am answering my own question so the community can find this answer if they need it.

In order to make data available from anywhere with js-ipfs using the browser, we need to pass the bootstrap nodes to IPFS:

     let opts = {
      repo: 'ipfs-testing321',
      config:{
       bootstrap: [
        "/dnsaddr/bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN",
        "/dnsaddr/bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb",
        "/dnsaddr/bootstrap.libp2p.io/p2p/QmZa1sAxajnQjVM8WjWXoMbmPd7NsWhfKsPkErzpm9wGkp",
        "/dnsaddr/bootstrap.libp2p.io/p2p/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa",
        "/dnsaddr/bootstrap.libp2p.io/p2p/QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt",
        "/dns4/node0.preload.ipfs.io/tcp/443/wss/p2p/QmZMxNdpMkewiVZLMRxaNxUeZpDUb34pWjZ1kZvsd16Zic",
        "/dns4/node1.preload.ipfs.io/tcp/443/wss/p2p/Qmbut9Ywz9YEDrz8ySBSgWyJk41Uvm2QJPhwDJzJyGFsD6",
        "/dns4/node2.preload.ipfs.io/tcp/443/wss/p2p/QmV7gnbW5VTcJ3oyM2Xk1rdFBJ3kTkvxc87UFGsun29STS",
        "/dns4/node3.preload.ipfs.io/tcp/443/wss/p2p/QmY7JB6MQXhxHvq7dBDh4HpbH29v4yE9JRadAVpndvzySN"
       ]
      }
     }
      const node = await Ipfs.create(opts)

The nodes above are current as of the date of this answer. But if you need the current nodes, you can retrieve them with:

      let bootstrap = await node.bootstrap.list()
      for(item in bootstrap){
       console.log(bootstrap[item].toString())
      }

Upvotes: 2

Related Questions