GUN Graph Database

repository·master·Indexed 12 days ago

https://github.com/amark/gun

A realtime, decentralized, offline-first, graph data synchronization engine designed for building multiplayer and encrypted applications. Version 0.2020.1239 provides a local-first P2P experience without a central authority, supporting key/value stores, tables, and documents with high performance.

Tokens
7.7K
Snippets
42
Records
47
Agent score
95%

What's inside GUN

  1. What is GUN?

    master

    GUN is an ecosystem of tools designed for building community-run and encrypted applications. It functions as a decentralized, local-first, and offline-capable graph synchronization protocol.

    Core Features:

    • Multiplayer by default: Real-time P2P state synchronization.
    • Graph Data Model: Supports key/value, tables, documents, and more.
    • Decentralized & Encrypted: End-to-end encryption with no single master server or single source of truth.
    • High Performance: Lightweight embedded engine capable of 20M+ API ops/sec in ~9KB gzipped size.
  2. How Gun works in React Native (Crypto bridging)

    master
    Gun requires a crypto module which is not natively provided by React Native. To enable sea.js functionality, this implementation uses a WebView (React Native browser) to bridge the crypto module from the browser environment to the global window object. This is achieved via a modified version of webview-crypto to ensure compatibility with sea and React Native.
  3. Enable non-standard legacy encoding with TextEncoder

    master

    Standard behavior only supports encoding to utf-8. To force encoding to a legacy encoding, pass the NONSTANDARD_allowLegacyEncoding: true option to the TextEncoder constructor.

    Warning: If the browser has a native TextEncoder implementation, this polyfill will not be used, and the non-standard option will likely fail. To force the polyfill in a browser with native support, nullify the global objects before loading the scripts:

    <!-- Force polyfill usage in browsers with native support -->
    <script>
    window.TextEncoder = window.TextDecoder = null;
    </script>
    
    <!-- Use non-standard legacy encoding -->
    <script src="encoding-indexes.js"></script>
    <script src="encoding.js"></script>
    <script>
    var uint8array = new TextEncoder(
      'windows-1252', { NONSTANDARD_allowLegacyEncoding: true 
    }).encode(text);
    </script>
  4. Install text-encoding via HTML script tags

    master

    To use this polyfill in a web page, clone the repository and include the following scripts. Note that encoding-indexes.js is required if you intend to support encodings other than UTF-8.

    <!-- Required for non-UTF encodings -->
    <script src="encoding-indexes.js"></script>
    <script src="encoding.js"></script>
  5. Install and run GUN examples

    master

    You can get started with GUN by installing it via npm and running the included examples locally. This is a quick way to see the library in action within your own environment.

    Prerequisites:

    • Node.js and npm installed.
    • If the npm command fails, you may need to run mkdir node_modules first or use sudo.

    Steps:

    1. Install the package: npm install gun
    2. Navigate to the package directory: cd node_modules/gun
    3. Run the examples: npm start
    npm install gun
    cd node_modules/gun && npm start
  6. Debug Gun in React Native

    master

    You can debug Gun by using react-native-debugger or Chrome's debugger. Once the debugger is attached, you can access the following Gun globals directly in the console:

    • gun: The root Gun instance.
    • user: The current Gun user instance.
    # iOS
    cmd+D -> Debug JS Remotely
    
    # Android
    cmd+M -> Debug JS Remotely
  7. Deploy a GUN relay peer on Linux

    master

    To quickly set up a GUN relay peer on a clean Linux installation with sudo privileges, you can use the provided installation script.

    Note: Review install.sh before running. If curl is not installed, manually copy the contents of the script into your SSH session.

    Common environment variables for deployment include:

    • HTTPS_CERT: Path to your SSL certificate.
    • HTTPS_KEY: Path to your SSL key.
    • PORT: The port to listen on (e.g., 443).

    Example setup:

    export HTTPS_CERT=~/cert.pem HTTPS_KEY=~/key.pem PORT=443
    curl -o- https://raw.githubusercontent.com/amark/gun/master/examples/install.sh | bash

    To stop the peer, use killall screen or killall node.

    curl -o- https://raw.githubusercontent.com/amark/gun/master/examples/install.sh | bash
  8. Run GUN tests and clear database

    master

    To run tests, ensure mocha is installed globally, then run npm test from the gun root folder.

    Important: Tests trigger persistent writes to the database. If subsequent runs fail, you must clear the database by running:

    rm -rf *data*
    npm install -g mocha
    npm test
    rm -rf *data*
  9. Deploy GUN to Heroku

    master

    You can deploy GUN to Heroku using the following steps.

    Warning: Heroku's ephemeral file system deletes data every 15 minutes. To prevent data loss, you should use Amazon S3 for storage.

    To deploy via CLI:

    git clone https://github.com/amark/gun.git
    cd gun
    heroku create
    git push -f heroku HEAD:master
  10. Scaffold Angular code

    master

    Use the Angular CLI to generate new project entities. Use the following commands to scaffold components, directives, pipes, services, classes, or modules:

    • ng generate component component-name
    • ng generate directive component-name
    • ng generate pipe component-name
    • ng generate service component-name
    • ng generate class component-name
    • ng generate module component-name
    ng generate component component-name