Skip to main content

Node.js Integration

GUN runs natively in Node.js and can be used as both a relay peer server and for server-side data operations.

Installation

1

Install GUN

Install GUN via npm:
2

Create a basic server

Set up a simple HTTP or Express server with GUN.
3

Configure persistence

Optionally configure file-based persistence or other storage adapters.

Basic HTTP Server

Here’s a minimal GUN relay peer using Node’s built-in HTTP module:

Express Integration

Integrate GUN with an Express.js application:

Production-Ready Server

A more robust setup with clustering and HTTPS support:

Hapi.js Integration

For Hapi.js applications:

Server-Side Data Operations

Use GUN on the server for data operations:

Environment Configuration

Use environment variables for configuration:

Storage Adapters

File Storage (Default)

Custom Storage Adapter

Connecting to Other Peers

Connect your relay peer to other GUN peers:

Process Management

Use PM2 for production process management:
ecosystem.config.js:

Docker Deployment

Dockerfile:
docker-compose.yml:

Best Practices

  1. Use clustering: Implement clustering for better reliability
  2. Enable persistence: Use file storage or custom adapters for data persistence
  3. Configure peers: Connect to multiple relay peers for redundancy
  4. Monitor resources: Track memory and CPU usage in production
  5. Implement logging: Add proper logging for debugging and monitoring
  6. Use HTTPS: Enable SSL/TLS for production deployments
  7. Set up backups: Regularly backup your data directory

Monitoring & Debugging

Next Steps