Fireship · @fireship
GUN Decentralized Graph DB in 100 Seconds
Uploaded June 7, 2021 · Length 2:19
Summary
Gun.js explained: decentralized graph database for real-time data syncing.
- gunjs
- decentralized database
- graph db
- webRTC
- realtime data
Transcript preview
The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.
- 0:00
Gun.js, a decentralized graph database for freedom fighters.
- 0:05
Unlike a centralized database that lives on a server hosted by big tech, data in Gun is distributed across multiple peers or users through the magic of WebRTC.
- 0:14
An end user like Bob requires a certain graph of data to use your app, while Alice requires an entirely different one with a few shared data points in between.
- 0:22
Each peer can store the data it needs in the browser's local storage, then sync changes with other peers in the network.
- 0:28
A peer might be one of your end users, or a relay server that you deploy to make the system more reliable.
- 0:33
That means the entire database is the union of all graphs on the network, and no one machine controls the entire system.
- 0:39
In other words, it's like a decentralized Firestore, where you get real-time updates, offline mode, and the illusion of low latency.
- 0:46
The data itself is managed through a minimal Graph API, where each record is a node that contains a unique ID or soul, and any other custom data for that entity in plain JavaScript.
- 0:57
Any node on the graph can reference another node, allowing you to create an infinite chain of circular dependencies,
- 1:02
like Bob might follow Alice who wrote a post that Bob commented on which she found offensive so she banned him An individual node never contains a duplicate of another node just a reference to it allowing you to model complex relationships without the need for a schema
- 1:16
Getting started with GunJS is incredibly easy.
- 1:19
Just install it with npm into your JavaScript project.
- 1:22
Make a reference to a node with the get method followed by a key for that location.
- 1:26
Then use put to write data to it.
- 1:28
That'll add a single node to the graph,
- 1:30
Then we can read it or listen to updates in real time using the on method.
- 1:34
Whenever this node changes on the network, the callback will be fired with the latest data.
- 1:39
We may also want to reference this node in a collection that can be easily queried.
- 1:42
For that, we can create a node called users, then define a mathematical set on it and add
- 1:47
each unique user to that collection.
- 1:49
We can then run a query against it or loop over it using map to implement our own sorting
- 1:54
or filtering logic in JavaScript.
- 1:56
In addition to the database API, it also contains tools for security, encryption, and authorization,


