Build on IPFS

IPFS has a few default services that it runs by default, such as the DHT, Bitswap, and the diagnostics service. Each of these simply registers a handler on the IPFS PeerHost, and listens on it for new connections. The corenet package has a very clean interface to this functionality. So let's try building an easy demo service to try this out!

Let's start by building the service host:

            package main

            import (
            	"fmt"

            	core "github.com/ipfs/go-ipfs/core"
            	corenet "github.com/ipfs/go-ipfs/core/corenet"
            	fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo"

            	"code.google.com/p/go.net/context"
            )
          

We don't need too many imports for this. Now, the only other thing we need is our main function:

Set up an IPFS node.

            func main() {
              // Basic ipfsnode setup
              r, err := fsrepo.Open("~/.ipfs")
              if err != nil {
                panic(err)
              }
            
              ctx, cancel := context.WithCancel(context.Background())
              defer cancel()
            
              cfg := &core.BuildCfg{
                Repo:   r,
                Online: true,
              }
            
              nd, err := core.NewNode(ctx, cfg)
            
              if err != nil {
                panic(err)
              }
          

That's just the basic template of code to initiate a default IPFS node from the config in the user's ~/.ipfs directory.

Next, we are going to build our service.

            list, err := corenet.Listen(nd, "/app/whyrusleeping")
	          if err != nil {
          		panic(err)
          	}

          	fmt.Printf("I am peer: %s\n", nd.Identity.Pretty())

          	for {
        	    	con, err := list.Accept()
        	    	if err != nil {
      		        	fmt.Println(err)
        	      		return
          	  	}

            		defer con.Close()

            		fmt.Fprintln(con, "Hello! This is whyrusleepings awesome ipfs service")
            		fmt.Printf("Connection from: %s\n", con.Conn().RemotePeer())
            	}
           }
          

And that's really all you need to write a service on top of IPFS. When a client connects, we send them our greeting, print their peer ID to our log, and close the session. This is the simplest possible service, and you can really write anything you want to handle the connection.

Now we need a client to connect to us:

            package main

            import (
              	"fmt"
              	"io"
              	"os"

              	core "github.com/ipfs/go-ipfs/core"
              	corenet "github.com/ipfs/go-ipfs/core/corenet"
              	peer "github.com/ipfs/go-ipfs/p2p/peer"
              	fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo"

              	"golang.org/x/net/context"
            )

            func main() {
            	if len(os.Args) < 2 {
            		fmt.Println("Please give a peer ID as an argument")
            		return
            	}
            	target, err := peer.IDB58Decode(os.Args[1])
            	if err != nil {
            		panic(err)
            	}

            	// Basic ipfsnode setup
            	r, err := fsrepo.Open("~/.ipfs")
            	if err != nil {
              		panic(err)
            	}

            	ctx, cancel := context.WithCancel(context.Background())
            	defer cancel()

            	cfg := &core.BuildCfg{
              		Repo:   r,
              		Online: true,
            	}

            	nd, err := core.NewNode(ctx, cfg)

            	if err != nil {
              		panic(err)
            	}

            	fmt.Printf("I am peer %s dialing %s\n", nd.Identity, target)

            	con, err := corenet.Dial(nd, target, "/app/whyrusleeping")
            	if err != nil {
              		fmt.Println(err)
              		return
            	}

            	io.Copy(os.Stdout, con)
            }
          

This client will set up their IPFS node (note: this is moderately expensive and you normally won't just spin up an instance for a single connection) and dial the service we just created.

To try it out, run the following on one computer:

            ipfs init # if you haven't already
            go run host.go
          

That should print out that peer's ID, copy it and use it on a second machine:

            ipfs init # if you haven't already
            go run client.go 
          

It should print out Hello! This is whyrusleepings awesome ipfs service

Now, you might be asking yourself: "Why would I use this? How is it better than the net package?". Here are the advantages:

  • You dial a specific peerID, no matter what their IP address happens to be at the moment.
  • You take advantage of the NAT traversal built into our net package.
  • Instead of a 'port' number, you get a much more meaningful protocol ID string.

There are 2 implementations of IPFS clients, one in JavaScript and the other in Go. JavaScript might seem like the best choice here, but since it’s in a much earlier state of development than the Go client, this is not the best choice. We’ll use the Go client and connect to it with Node via its API.

Prerequisites:

  • Node.js (ideally one of the later versions) & npm
  • A go-ipfs client (installation instructions here)
  • Postman or curl, for making requests to our REST API

First of all, our node has to be running in online mode, so open up a terminal and run ipfs daemon. You should see something like this:

On line 19, you can see the API server is listening on port 5001. This is what we need to connect to.

Secondly, create a new project directory and run npm install ipfs-http-client. This is the package we need to connect to our running IPFS node.

Next, let’s create the js file where we’ll connect to the node:

On line 3 we actually connect to the daemon API server. We can now start executing commands on the ipfs object to interact with the network.

As an example, let’s write a function that adds some text to IPFS:

On line 1, we create an object to add to IPFS. The path is what we want the file to be called on IPFS (we can include a directory), and the content is a Buffer of the file (in this case, just plain text) we want to add. Next, we add the file to ipfs with ipfs.add(), which returns an array with all the added files. Since we only added one, the result of console.log() will be:

If you’re following along, you’ll notice that the hash field will be exactly the same every time, because you added the same content I did. Also, notice that the path name doesn’t affect the content identifier. If you now want to retrieve your content, you have 2 possibilities:

  • Use your local gateway server like this: http://localhost:8080/ipfs/QmWfVY9y3xjsixTgbd9AorQxH7VtMpzfx2HaWtsoUYecaX The gateway server was spun up by your IPFS daemon.
  • Use a public gateway like this: https://gateway.ipfs.io/ipfs/QmWfVY9y3xjsixTgbd9AorQxH7VtMpzfx2HaWtsoUYecaX

All this is already pretty cool, but let’s see how we can use this in an app. To demonstrate some more functionality, let’s create a small REST API with Express.

Building An Application

Don’t forget to run npm install express.

Let’s start with some boilerplate code:

Go ahead and run this and test it out with curl http://localhost:3000. You should see Welcome to my IPFS app.

Let’s now add a POST route:

We can now test out this route with Postman. Create a new POST request to http://localhost:3000/upload. You can choose what you put in the body, but it has to be JSON:

If everything went well, you should’ve gotten the response I got and some terminal output from our Express app: { path: 'postman request', content: 'postman says whassup' } . Since we use JSON middleware, req.body got parsed as a JS object, which is already the format we need to add something to IPFS. Let’s extend the functionality by modifying and calling the addFile function, and we’ll return a link to the added file over a public gateway:

In addFile() on line 8, we take in the req.body data as a parameter and use that to add to IPFS. We then return the fileHash, so that we can include it in the link we send back as a response. If we now make another POST request on Postman:

We get back a link to view our file on a public gateway! Note that this link might actually take a while to load since public gateways can be very slow.

That was it. Thanks for reading.