IPFS on the server. Host sites from a laptop



I often need to publish a static page or website, a demo with a web form or layout. Pouring somewhere like Jsfiddle every time is not always convenient, and editing statics in a localhost is much faster and more pleasant. Problems begin when I need someone to show my work, or just open the same page from the phone. We have to host all these endless working options and sketches, for each upload files again, screw vhosts.

Using IPFS, you can host sites on the Internet directly from your laptop, all updates of local files will be immediately applied on the Internet, and they do not need to be uploaded somewhere. When the laptop is disconnected from the network, the site will still be available. IPFS is like Bittorrent, just for the web.

In the article, we will deploy the IPFS node on the server and try this technology in practice.

What is IPFS?


IPFS is a large, decentralized p2p network that is used as a file sharing service, web archive or Bittorrent replacement . All the cool examples of using IPFS in real projects can be found in the hall of fame on the official awesome.ipfs.io website .

Briefly, it works like this: stored files get their multicash and are divided into blocks that are scattered across all the interested nodes. DHT is synchronized on the nodes; when a file is downloaded, blocks are collected from different (in theory, the nearest) nodes. Moreover, to access the file or directory you do not need to raise your node, they are all accessible from the browser, which leads us to the popular feature: you can host sites on IPFS for free. But only statics, updating any data more than once every few minutes is inconvenient because of the same hashes that form a link and change every time the file is changed (there is a permanent IPNS name system, but it is slow). However, this did not stop the dudes from Orbitdb from flashing the database to IPFS , but there are some nuances. Read more about the network device here..


Suppose I have a node with which I distribute static sites for free, I go to them myself and sometimes bring my colleagues to look. The total amount of data is limited only by the size of my disk, and the download speed by the home Internet, what could be better? But there are a number of problems. Firstly, IPFS itself needs quite a bit of Internet and a solid piece of the processor, and even without traffic, it always takes some of the resources for DHT synchronization. Secondly, I mainly work with a laptop and keep all the files on it, and therefore it’s far from always that I have a homemade half-gigabyte fiber at hand. Short breaks for IPFS is not a problem, it keeps the cache in DHT for several hours, but it costs somewhere to leave for a couple of days, and now all your projects are happily spilling out of the network. You can stomp (“remember”) files on the desktop, but this will at least double the traffic,which is also not comme il faut. What to do? I tried to raise the node on the server to unload the laptop, but I still had to upload the files manually, like on a regular hosting. As a result, I smoked docks and APIs and wrote a simple utility to synchronize my local statics with the server.



IPFS has two separate implementations: go-ipfs and js-ipfs. JS is closer to me, so I wrote on it. I wanted the utility to be able to pick up folders with my sites, and regularly upload them to the network while I work. The server side should catch the hashes of the folders and kick them so that the files are not lost.

Installation and launch


npm install ipfs -g

Js-ipfs has a pretty detailed tutorial with examples , so:

git clone https://github.com/ipfs/js-ipfs.git
cd js-ipfs
npm install
npm run build

The node runs in several lines:

const IPFS = require('ipfs')
async function main () {
  const node = await IPFS.create()
}
main()

Configs and seeds for it are registered right there, but this is enough to start.

We write functionality


Next, you need to transfer the files to the node and upload them to IPFS. To do this, use node.addwith the option { recursive: true }for folders. The address can be passed in arguments at startup, and saved by command. It is important to record only the last hash - it is from the root folder:



The entire folder has successfully gone to the network. The site will be opened via the link, and the folder itself can be checked on the IPFS webmord:



Next, to make the saving process convenient, I added the saving with the version number and publication in IPNS vianode.name.publish. But saving manually is boring! I want to be able to watch the updated site as quickly as I see changes in the localhost, which means that the update should happen automatically. And if I suddenly forget to save something, turn off the laptop and go home, the current version will be saved not only in the editor, but also on the network. We will make auto-saves by default once every 10 minutes, with the ability to change the interval for different rates of work. By the way, if the files have not changed since the last save, the hash will not change either.



Cool, but so far we are distributing all the files from the local machine. It's time to connect the server node! We take the experimental pubsub , get the topic from the arguments at startup and try to deliver the save hash:



Hurray! The small thing is to make the server store all received hashes and kick them (node.pin.add), and teach the client to cut down his node when it is not needed ( node.stop).


This is the list of downloads on the server node

What is the result?


  • When I sit down to write code, it is automatically saved in IPFS
  • All versions are always available via personal links / ipfs / Qm ...
  • I can publish the site in IPNS so as not to send a bunch of links to the client
  • The local node wakes up only to upload files and contact the server, then it falls asleep back
  • Locally, I have only one copy of the site, the traffic to single downloads of new versions is much less than the background traffic of the IPFS node
  • I can finally work in a localhost without a headache with versioning

All this happiness costs me the price of the cheapest VPS, there are still enough resources and should be enough somewhere else for a year ahead, after which it will be possible to buy another server or archive the old one.

In general, using IPFS, you can do much more interesting things. I definitely want to bring this utility to mind and write a new one, with gooey. In the meantime, keep the site that we wrote and hosted all this time:

https://ipfs.slipner.ru/ - with the domain
https://ipfs.io/ipfs/QmeV7MVFSiWJr8u4dhXzEAoJgbVJsHvSUqjsbQyNL4WrD2 - IPFS
https ://gt / ipns / QmTjKBMJS8owPUXQFSMjoR4kqFNXXVKB7DqNGgyqibxuff / - IPNS via the official gate

Opera browser with IPFS support




Just a few days ago, Opera Software rolled out the first browser with native IPFS support. So far, however, only in the mobile version for Android. This means that it can directly access the IPFS network without web gateways! We are waiting for the support to be added to the desktop version.




All Articles