RaspberryPi - amark/gun GitHub Wiki

Host a GunDB Relay Peer on your Raspberry Pi! Even from home!

This guide assumes the use of a Raspberry Pi running Raspbian, but should be adaptable to any Linux system.

Setup

As the pi user, here is a simple manual example installation:

mkdir -vp ~pi/gun-peer
cd ~pi/gun-peer
npm install "https://github.com/amark/gun.git" --save
cd node_modules/gun
npm start

Reference this gist for automatic setup of GunDB with systemctl service creation.

Detailed Tutorial

Run your Node.js application on a headless Raspberry Pi https://dev.to/bogdaaamn/run-your-nodejs-application-on-a-headless-raspberry-pi-4jnn

Here is a simple gun relay server.

Start a new repo:

// server.js

;(function(){
  var gun = require('gun/examples/http');
  if(!gun.back){ return } // http example auto spawns subprocess

  var fs = require('fs');
  var server = gun.back('opt.web');
  var route = server.route = {}

  fs.readdir('./route', function(err, dir){
      if(err || !dir){ return }
      dir.forEach(function(file){
          if(!file){ return }
          route[file.split('.')[0]] = require('./route/'+file);
      });
  });
 
// with this line you can type a message on http://localhost:8765/basic/paste.html and check if the server works.
  gun.get('test').on(data => console.log(data))

}());

-npm gun

-npm start

Exposing to the Public Internet

If you are behind a NAT, follow along below for some options on how to expose the service publicly.

Static Local IP

It is important that the device have a static local IP like 192.168.0.50 rather than to be assigned a random address lease with DHCP on boot so the router can consistently forward traffic. To set a static IP either configure the network configuration in the Pi or use the router to specify DHCP Reservation for the Pi's MAC address so the router always gives it the same IP.

Port Forwarding

Port forwarding is a process of configuring the router to forward inbound traffic to a specific device and port on your local network. Log into your home router as admin and create a port forward for TCP 8765 to TCP 8765 of the IP address of the machine hosting your Gun instance (you can run ip addr in terminal on the machine to see your IP).

For more specific instruction, reference the noip.com's tutorial article or search for something like "Port Forwarding Router Tutorial"

Port Forwarding with UPnP

To port forward with UPnP automatically, we can use miniupnpc (replace 192.168.0.50 with your Pi address):

sudo apt update 
sudo apt install miniupnpc
upnpc -e "GunDB Relay Peer" -a 192.168.0.50 8765 8765

Dynamic DNS

Dynamic DNS provides a convenient domain that is constantly updated to your latest public IP which is very likely to change in a residential ISP setting. Dynamic DNS can be set up in any of several ways. Here are some references:

https://www.noip.com/integrate/request (free user account neccessary)