Electron Integration
GUN works excellently with Electron, enabling you to build decentralized desktop applications with offline-first capabilities and peer-to-peer synchronization.Installation
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:Using with Electron Forge
If you’re using Electron Forge: package.json:Preload Script (Optional)
For better security with context isolation: preload.js:Data Persistence
GUN data is stored in Electron’s userData directory:- 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:Best Practices
- Run GUN in main process: Keep the relay peer in the main process for better control
- Use userData path: Store GUN data in Electron’s userData directory
- Enable context isolation: Use preload scripts for security
- Handle app lifecycle: Properly cleanup GUN connections on app quit
- Sync with remote peers: Connect to external peers for cross-device sync
- Implement auto-updates: Keep your app and GUN version up to date
Example Projects
- GUN Electron Test - Basic Electron + GUN setup
- Check the GUN examples directory for more patterns
Next Steps
- Learn about GUN’s SEA for encryption
- Explore peer-to-peer networking
- Set up remote relay peers
- Build offline-first features