Skip to main content

Gun.js

What is gun.js?

Gun.js is an open-source and realtime, decentralized, offline-first, graph database engine written in JavaScript. It is a distributed system for synchronizing data across web applications and devices.

Image Source : Github

The database is formed by the user's information stored on his own machine, on other users' machines and on larger servers in the system, which allows the information to remain reliable and prevents loss of information if a user's device is lost.

It's an ecosystem of tools that let you build community run and encrypted applications - like an Open Source Firebase or a Decentralized Dropbox.

Under the hood, GUN allows for data synchronization to happen seamlessly between all connected nodes by default. It’s offline first capabilities mean that if connectivity is lost to other nodes due to a network error or no availability, the application will store all changes locally and automatically synchronize as soon there is a connection.

Finally, the flexible data storage model allows for tables with relations (MSSQL or MySQL), tree-structured document orientation (MongoDB), or a graph with circular references (Neo4j).

Idea behind gun.js

Gun.js is based on the DWeb concept, which stands for decentralized internet.

DWeb is about the web, so it's not surprising that it comes from the concept of community. Soon all users will have access to all the information because it will be stored on the devices of all of them. Each piece of information is stored alongside the free Gun.js servers.

This represents a huge benefit for users because it lowers the costs of programmers and makes the internet far more accessible.

Since large companies today are responsible for taking a large part of the money that goes into the system and also able to freely access a large part of the data without having to know for sure whether we are providing data to be spied on or not.

The database is made up of user data stored on one's own machine, on the other's machine and on larger servers on a system that makes information reliable and prevents information loss if the device of a user is lost. Not only is it a unique concept, but it's a great one, too.

Installation

GUN can be used in both browsers and servers.

  • Browser

    There are a couple choices:

  • Script Tag

    The easiest is to just add GUN into your HTML:

      <script src="https://cdn.jsdelivr.net/npm/gun/gun.js"></script>
    <script>
    // your code here
    </script>
  • Require

    Assuming you are using something like Webpack or Browserify, first follow the npm install, then add this to your browser code:

    var Gun = require('gun/gun');
  • Import

    Same as with require, but using the latest ES6 syntax:

    import Gun from 'gun/gun'

    Note: For now, even though you import it, GUN is still defined and used as a global variable.

  • Node

    First you need to install GUN with NPM or Yarn via the command line:

    $npm install gun or $yarn add gun

    Note: If you don't have node or npm installed, read this.

    Then add this to your server code:

    var Gun = require('gun');

    Note: GUN comes with many default NodeJS adapters for storage and networking. If you do not want these, just do require('gun/gun') instead.

  • Server

    If you want to actually install GUN to a server, you need to pass it an HTTP instance:

    var server = require('http').createServer().listen(8080);
    var gun = Gun({web: server});

Advantages of gun.js

GUN gives us the most powerful digital weapons on the internet: encryption for privacy, and independence through decentralization. There are now enough phones on the planet to power all of Facebook by the people, for the people, and of the people.

The aim of GUN is to let you focus on the data that needs to be stored, loaded, and shared in your app without worrying about servers, network calls, databases, or tracking offline changes or concurrency conflicts. This lets you build things like chat apps, games, or any type of app that requires real-time updates.

GUN apps are fully decentralized, meaning that changes aren't controlled by any central server. Servers are usually just another peer in the network, one that may have more reliable resources than a browser. You save data on one machine, and it will sync it to other peers without needing a complex consensus protocol.

Gun stores your data across all connected peers in the network. Each peer may own the complete graph, or just a subset of the complete graph, or it may possess data that does not yet exist on any other node. The whole database is considered to be the union of all peers' graphs.

Example

In this example we're going to build a P2P multi-user public Todo App!

<html>
<body>
<h1>Todo</h1>

<form id="sign">
<input id="alias" placeholder="username">
<input id="pass" type="password" placeholder="passphrase">
<input id="in" type="submit" value="sign in">
<input id="up" type="button" value="sign up">
</form>

<ul></ul>

<form id="said">
<input id="say">
<input id="speak" type="submit" value="speak">
</form>

</body>
<script src="https://cdn.jsdelivr.net/npm/gun/examples/jquery.js"></script>
<script src="https://cdn.jsdelivr.net/npm/gun/gun.js"></script>
<script src="https://cdn.jsdelivr.net/npm/gun/sea.js"></script>
<!-- script src="https://cdn.jsdelivr.net/npm/gun/lib/webrtc.js"></script -->
<script>
// Todo App code will go here!
</script>
</html>

Gun is changing the way that we think about databases and is slowly transitioning us to a new paradigm. Gun, and its related projects, have a lot of aspects that are very different from the classical centralized models. You may find it challenging to learn Gun if you are just getting started. Firstly, because Gun is a young project and you should expect the APIs to change. And secondly, you may find it difficult to wrap your head around the documentation.

References:-