Skip to main content

Electron Integration

GUN works excellently with Electron, enabling you to build decentralized desktop applications with offline-first capabilities and peer-to-peer synchronization.

Installation

1

Set up Electron project

Initialize your Electron project:
2

Install GUN

Add GUN to your project:
3

Configure main and renderer processes

Set up GUN in both Electron’s main process (Node.js) and renderer process (browser).

Project Structure

A typical Electron + GUN project structure:

Main Process Setup

Set up GUN in Electron’s main process to run a local relay peer: main.js:

Renderer Process Setup

In the renderer process, connect to the local GUN peer: renderer.js:
index.html:

Using with Electron Forge

If you’re using Electron Forge: package.json:

Preload Script (Optional)

For better security with context isolation: preload.js:
Then in renderer:

Data Persistence

GUN data is stored in Electron’s userData directory:
Data location by platform:
  • Windows: %APPDATA%\<app-name>\gun-data
  • macOS: ~/Library/Application Support/<app-name>/gun-data
  • Linux: ~/.config/<app-name>/gun-data

User Authentication

Implement user authentication with GUN’s SEA:

Multi-Window Synchronization

Sync data between multiple windows:

Building & Distribution

Build your Electron app with GUN:
electron-builder configuration:

Best Practices

  1. Run GUN in main process: Keep the relay peer in the main process for better control
  2. Use userData path: Store GUN data in Electron’s userData directory
  3. Enable context isolation: Use preload scripts for security
  4. Handle app lifecycle: Properly cleanup GUN connections on app quit
  5. Sync with remote peers: Connect to external peers for cross-device sync
  6. Implement auto-updates: Keep your app and GUN version up to date

Example Projects

Next Steps