Using IPFS with electron (or muon) #256
Description
It was raised to my attention that there have been some questions on the state of IPFS in Electron. I thought I would compile my notes and the information I shared in the last IPFS All Hands as it might be used by several members of the community.
If you are new to Electron or Muon, they are both open source frameworks that enable you to create native desktop applications that are independent of the OS (as long as it is Linux, Windows or Mac) while using the familiar Web technologies that you know and love. Electron was the first and Muon is a fork designed to specially build browsers.
Both of these frameworks offer to contexts, the:
- Main process - A process that runs inside a Node.js context. This means every Node.js core module is available for you to use, including the FileSystem and a regular Network Stack.
- Renderer process - The process where the UI of the application runs. This has a very similar feel to a regular browser application.
Both of these processes are connected by an IPC channel (ipcMain and ipcRenderer) so that you can send messages back and forth.
Given all of this, what are the options today? They are:
- 1) Using a go-ipfs daemon with js-ipfs-api from the Main process
- 2) Using a go-ipfs daemon with js-ipfs-api from the Renderer process
3) Using js-ipfs from the Main processCurrently, it does not work and that is what the thread The road to Electron support covers.- 4) Using js-ipfs from the Renderer process
1) Using a go-ipfs daemon with js-ipfs-api from the Main process
Spawning a go-ipfs daemon from Node.js is quite easy today by using the ipfsd-ctl
. This module lets you export two API, one to directly control the daemon, another to control the daemon using js-ipfs-api
.
We use this mode on Station, the IPFS menu bar application. You can also learn how we spawn the node and interact with it on the init.js file.
2) Using a go-ipfs daemon with js-ipfs-api from the Renderer process
Very similar to the mode above, you can spawn a go-ipfs deamon through the Main process and then create a js-ipfs-api on the Renderer process to make requests to it.
Having to create the instance separately could feel a bit cumbersome, fortunately, there is a better way! Electron supports a way to call Main process modules or instances directly from the Renderer process by automatically creating an RPC for them using the Remote API. You are able to create the same instance as 1) and then pass a reference to the Renderer process without having to actually do instantiation on the Renderer process.
This is how orbit-electron has been working since the beginning https://github.com/orbitdb/orbit-electron
3) Using js-ipfs from the Main process
Currently, it does not work and that is what the thread The road to Electron support covers.
The reason why is due to the use of some Native dependencies in js-ipfs that do not get compiled cleanly by Electron's own Node.js version. We already started the process of fixing or replacing some of this dependencies but more work needs to be done. If you would like to help, make sure to check The road to Electron support.
Update: With js-ipfs 0.29 and Electron 2.0, you can now run js-ipfs on the Main Process too! See an example here https://github.com/ipfs/js-ipfs/tree/master/examples/run-in-electron.
4) Using js-ipfs from the Renderer process
You can use js-ipfs from the Renderer process. The simplest way is to just grab the dist version of js-ipfs
and add it as a script to your Electron app, it will expose a Ipfs
class for the context of your app just like loading IPFS through a Script tag.
This is own we approached the IPFS integration for the Brave browser as you can see in the prototype ipfs-inactive/browser-laptop#1