So you want to start coding blockchain? … IPFS & OrbitDB [4.1]

In the previous posts we have gone through several steps in learning how to code blockchain.

  • Step 0: describes how efficient are the modern development tools, with a hands-on example to code and deploy your own web server and its client in only 10 lines of code.
  • Steps 1, 2 and 3: (1) explains MetaMask and how to install and use it to sign and pay transactions, (2) explains installation of nodeJS, truffle, git, (3) introduces to VSCode.

We continue this journey here with IPFS and OrbitDB, through a short JavaScript application. In the same approach as in Step 0, this application shows how to leverage existing powerful libraries to create and use a peer-to-peer decentralised database with only a few lines of code.

What will we learn today?

Instead of proceeding chronologically, the plan of this tutorial innovates as follows:

  • We start with explaining the code itself, in case you have already done the steps above and have your development environment.
  • For those who have not, we repeat how to set it up. If you are really new, jump to this section and then come back to run the code part.
  • Last but not least, we’ll talk about IPFS and OrbitDB design principles and see how they differ from legacy databases. If you are a Manager or a System Architect, you may want to skip the coding and jump directly here. We’ll also explain how in project Machu Picchu we use IPFS and OrbitDB to complement the blockchain.

The application (kind of) “Hello World” storyboard

The code

  • First, install the relevant npm packages. For this, navigate to in your working directory and in your console (Terminal on MacOS), type this command: npm install ipfs-http-client orbit-db
  • In your favorite text editor, copy-paste the following JavaScript code
  • save the file as index.js
  • in your console, type the following command: ipfs daemon --enable-pubsub-experiment & node index.js
  • the output should be something similar to the following

Congratulations. You have spawned an IPFS node, created a peer-to-peer key-value database, written 2 entries and read them back. That’s all folks.

What have you done?

  • You have installed 2 JavaScript packages to use IPFS and OrbitDB. The ipfs package includes a daemon that can be called from the command line to create an IPFS peer client
  • You have launched the IPFS daemon to run in background an IPFS client, also called a “peer” or a “node”.
  • You have used nodeJS to run your index.js code. NodeJS has nothing to do with the IPFS node. You’ll see peers and nodes everywhere when programming blockchain and IPFS 🤓).

You may want to pursue and read more about IPFS and OrbitDB and see how their design differ from legacy databases.

Explanation of the code

  • line 6 imports the package ipfs.
  • line 7 imports the package orbit-db.
  • line 9–32 declare an asynchronous function, that is executed on line 33.
  • line 11 creates a JavaScript JSON to interface the API exposed by the spawned IPFS node.
  • line 13 creates a JavaScript JSON with all methods to handle a peer-to-peer distributed database on IPFS.
  • line 16 creates a key-value database that is actually an IPFS file.
  • lines 19-20 write 2 key-value pairs in the file.
  • line 21 closes the database (stops replication?).
  • lines 25–31 reopen the database and read data.

Reminder: how to install your development environment

Setup instructions for MacOS

  • Install Brew
  • Install nodeJS via brew: brew install node@14.17.0 (this version of nodeJS is the last long term stable support)

Setup instructions for Linux Ubuntu

curl -fsSL | sudo -E bash -
sudo apt-get install -y nodejs

Under Windows, it is strongly recommended to run a VirtualBox for Windows and install a Ubuntu Virtual Machine inside it. Here is a YouTube video of 4 minutes, that can guide you through this double installation process:

Next step: how to design a peer-to-peer decentralised database using IPFS & OrbitDB

See the second part here:



Machu Picchu — Data as a Public Service

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store