3

I'm using js-ipfs server side to 'upload' files on IPFS, but it seems like it's way less efficient than the command line ipfs daemon and ipfs add someFile.

Server-side, I instanciate an Ipfs object and then I try to storeData with a path and the buffer of the content of the file. I am connected to three swarms when my server starts:

Swarm listening on /ip4/127.0.0.1/tcp/4003/ws/ipfs/QmTBoDWXviw7eRKeDGbp3wU2PxAw1epPxax6mp8uY6CEEW
Swarm listening on /ip4/127.0.0.1/tcp/4002/ipfs/QmTBoDWXviw7eRKeDGbp3wU2PxAw1epPxax6mp8uY6CEEW
Swarm listening on /ip4/10.19.77.89/tcp/4002/ipfs/QmTBoDWXviw7eRKeDGbp3wU2PxAw1epPxax6mp8uY6CEEW

I use IPFS this way :

const IPFS = require('ipfs');
const node = new IPFS();

class Ipfs {

    constructor() {
        node.on('error', e => console.log(e))
        node.on('start', () => console.log('Node started !'))
        node.on('ready', () => console.log('Node is ready !'))
    }

    storeData(content, path) {
        return new Promise((resolve, fail) => {
            let encData = {
                path: path,
                content: content,
            };
            node.files.add(encData, (err, data) => {
                if (err) return fail(err);
                return resolve(data);
            })
        })
    }
}

When I call storeData from my object, IPFS gives me a hash back where I can get my file, but the https://ipfs.io/ipfs/[hash] never loads.

I want to insist on the fact that the hash given by the ipfs add command line works instantly

Thanks in advance

Yooooomi
  • 895
  • 1
  • 8
  • 20

1 Answers1

5

From the command line, when you run ipfs add <some file> it will chunk the file, and add it to your local ~/.ipfs directory. As it runs, it print out the CIDs of the chunks, and finally a root CID.

If you try to fetch a file from IPFS by running ipfs cat <root CID> with the CID you just got from running ipfs add then it will also be fast as it will be pulling the blocks from your local repository. If you run ipfs cat with a CID that you dont currently have, then it will ask your connected peers for it. This can take a while if are not currently connected to someone who is providing the data for that CID.

In js-ipfs calling node.files.add works similarly to running ipfs add from the command line. It will return the root CID as soon as it has finished chunking the files and storing them locally (in ~/.jsipfs by default).

Trying to fetch a root CID from the public gateway via https://ipfs.io/ipfs/[CID] can be slow if the IPFS nodes running on the gateway machines operated by ipfs.io take a while to find a provider for that CID. It's a remote machine, and it has to first find your local peer that is providing that CID, and fetch the data from them.

When you start up your ipfs node with ipfs daemon, the initial output tells you which local network interfaces and ports your nodes is listening on, so

Swarm listening on /ip4/127.0.0.1/tcp/4003/ws/ipfs/QmTBoDWXviw7eRKeDGbp3wU2PxAw1epPxax6mp8uY6CEEW
Swarm listening on /ip4/127.0.0.1/tcp/4002/ipfs/QmTBoDWXviw7eRKeDGbp3wU2PxAw1epPxax6mp8uY6CEEW
Swarm listening on /ip4/10.19.77.89/tcp/4002/ipfs/QmTBoDWXviw7eRKeDGbp3wU2PxAw1epPxax6mp8uY6CEEW

...means that your local ipfs node is listening on the ipv4 addresses 127.0.0.1 and 10.19.77.89. It doesn't tell you how many peers you have. To find out how many peers you have, run ipfs swarm peers

$ ipfs swarm peers
/ip4/1.64.206.172/tcp/42707/ipfs/QmUDGjCUDePrxWbpohHLuW8Hy1uGKXi4tJXaN6z7FuNszb
/ip4/100.24.159.221/tcp/46757/ipfs/QmQjGfZq3MvgykH5oXeQaZvU6TGMCD7xtRtUXz1sK4u8nG
/ip4/101.200.58.57/tcp/4001/ipfs/QmRXcRwhbjtgE2E3hF4rRhM3xaGRrjfieUE5iXGCAwrZ9d
...

...and to just find the number of peers you are connected to, you can pipe the output into wc -l

$ ipfs swarm peers | wc -l
     755

...and in answer to your fundamental question js-ipfs is only slower than go-ipfs due to the differences in the languages. The are both implementations of the same spec, and they both implement using similar algorithms. If you'd like to use go-ipfs from JS, for any reason, you can do that too, by running a go-ipfs daemon and talking to it via the HTTP API using the js-ipfs-http-client library which has the same api as js-ipfs

olizilla
  • 436
  • 4
  • 6