Fireship · @fireship

GUN Decentralized Graph DB in 100 Seconds

Uploaded June 7, 2021 · Length 2:19

GUN Decentralized Graph DB in 100 Seconds

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.

  1. 0:00

    Gun.js, a decentralized graph database for freedom fighters.

  2. 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.

  3. 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.

  4. 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.

  5. 0:28

    A peer might be one of your end users, or a relay server that you deploy to make the system more reliable.

  6. 0:33

    That means the entire database is the union of all graphs on the network, and no one machine controls the entire system.

  7. 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.

  8. 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.

  9. 0:57

    Any node on the graph can reference another node, allowing you to create an infinite chain of circular dependencies,

  10. 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

  11. 1:16

    Getting started with GunJS is incredibly easy.

  12. 1:19

    Just install it with npm into your JavaScript project.

  13. 1:22

    Make a reference to a node with the get method followed by a key for that location.

  14. 1:26

    Then use put to write data to it.

  15. 1:28

    That'll add a single node to the graph,

  16. 1:30

    Then we can read it or listen to updates in real time using the on method.

  17. 1:34

    Whenever this node changes on the network, the callback will be fired with the latest data.

  18. 1:39

    We may also want to reference this node in a collection that can be easily queried.

  19. 1:42

    For that, we can create a node called users, then define a mathematical set on it and add

  20. 1:47

    each unique user to that collection.

  21. 1:49

    We can then run a query against it or loop over it using map to implement our own sorting

  22. 1:54

    or filtering logic in JavaScript.

  23. 1:56

    In addition to the database API, it also contains tools for security, encryption, and authorization,