Skip to content
This repository has been archived by the owner on Apr 28, 2022. It is now read-only.

Earthstar Pub on a Raspberry Pi

Judy Tuan edited this page Apr 30, 2021 · 15 revisions

This was tested on a Raspberry Pi 3 Model B running a fresh install of "Raspbian 10 (buster)" in Feb 2021.

Things to know: http vs https

Earthstar pubs are just regular websites served from node using express.

Earthstar browser apps, and Earthstar pubs, have to both run on http, or both on https, or they can't talk to each other because of browser restrictions.

If you run your own apps locally, and your own pubs locally, they'll both be http which will work.

If you want to use apps deployed to the internet, those will be https, so you'll have to make your own pub reachable over https. There are several ways to do this:

  1. Run on a Raspberry Pi with some kind of self-signed cert or Let's Encrypt for use on your local network: Raspberry Pi with Let's Encrypt and SSH tunneling
  2. Run on a Raspberry Pi and expose to the internet through a service like ngrok or pagekite, which give you an https address and let anyone on the internet reach you
  3. Run a pub on a hosted service like glitch.com
  4. Run a pub on your same actual computer you're using, not a raspberry pi, because localhost is exempt from these https rules.

This document is about approach #2, Raspberry-Pi-And-Ngrok. The main README talks about #3, Glitch.

However you run a pub, http or https, you can always use the command-line tool earthstar-cli to sync the pub to a local folder of sqlite files for backup purposes. All of this http vs https stuff is just a browser limitation.

Set up the system

Start with a fresh install of Raspbian on your Raspberry Pi. Boot into the desktop view.

First, set a strong password from Start > Preferences > Raspberry Pi Configuration.

Then update the system software:

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install git gcc g++ make

Install Node

I used version 12 but it's a little old at this point, consider 14 instead.

curl -sL https://deb.nodesource.com/setup_12.x | sudo bash -
sudo apt install nodejs
node --version
npm --version

Optional: install yarn

Only needed if you're developing code, not needed if you're just running a pub.

curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt-get update && sudo apt-get install yarn
yarn --version

Set up npm global permissions

Out of the box, you can't run npm install --global because of permissions problems. So let's make our own place in our home directory to save globally installed stuff.

mkdir ~/.npm-global
npm config set prefix ~/.npm-global
echo 'PATH="$HOME/.npm-global/bin:$PATH"' >> ~/.profile
source ~/.profile 

Verify that npm global install works

npm install -g cowsay
cowsay hello

Install earthstar-pub

npm install --global earthstar-pub

This will take some minutes because it has to compile sqlite.

When it's done, verify that it worked and see the command line options for earthstar-pub:

earthstar-pub --help

Set up earthstar-pub

Make a directory for it

mkdir ~/mypub
mkdir ~/mypub/data
cd ~/mypub

Make a little shell script to launch it with your favorite options. Save this into run-pub.sh:

#!/bin/sh

earthstar-pub --port 3333 --sqlite --dataFolder ~/mypub/data

Make the script executable

chmod u+x run-pub.sh

Run it

Run it!

./run-pub.sh

View it at http://localhost:3333/ on the raspberry pi's browser to see the pub's web interface.

To use it on another computer on the same network, use http://raspberrypi.local:3333/ or figure out the Pi's IP address.

Find the IP address Run ifconfig and look in the wlan0 section (if it's on wifi) or the eth0 section (if it's on ethernet).

Use it from your local network

In any Earthstar app, you can now add http://raspberrypi.local:3333 as a pub. It will only work when your computer is on the same network as the raspberry pi.

If the app itself is being served over https, then the pub also needs to be on https or the browser will refuse to connect to it. The easiest way to do this is to set up a tunnel service (see the next section).

Use it over the internet

Use a tunnel service to let anyone on the internet access your raspberry pi.

I haven't tried any of these. Make sure you configure a consistent URL for your service so it's not constantly changing.

Tell your friends to use it

Tell your friends to use your pub -- there is no automated pub discovery, it works by word of mouth.

Also note that pubs don't sync directly with each other, yet -- instead, clients (browser apps) can sync with multiple pubs and this causes those pubs to stay in sync with each other via the clients.

Keep it running

To keep it running more reliably, especially if you're SSHing into the Pi, consider running it in a screen session.

To launch it every time the Pi reboots, try using pm2. Details in another section below.

Maintain it

Keep an eye on the ~/mypub/data folder to see which workspaces exist and how big they are.

Stop the pub temporarily if you want to delete workspace files from the /data folder. The workspaces will probably come back because users will sync them again. There is not yet a blocklist for workspaces -- PRs accepted

The pub does not write log files so there are none of those to clean up.

Back up your data

You can use the command-line tool earthstar-cli to sync workspaces to/from your pub to another computer. It will make one sqlite file for each workspace, just like the pub does.

Using pm2 to run services persistently

pm2 is a tool to run scripts in the background all the time, make them start on startup, and restart them if they crash.

Install pm2

npm install --global pm2

Set up your 2 services (pub and tunnel service)

I've skipped over setting up a tunneling service; in this case I've already set up pagekite.

pm2 start run-pub.sh --name pub
pm2 start pagekite.py

Learn to manage your processes

# see your processes
pm2 ls

# start or stop them
pm2 stop pub
pm2 start pub

# see more details about one
pm2 describe pub

# print logs
pm2 logs
# show fancy interface with logs
pm2 monit

Make it restore the processes on reboot

Make it run when you reboot: follow the instructions this gives you:

pm2 startup

Then run pm2 save to save your current process list and stop/started state, to be restored at every reboot. You only have to do this once.

pm2 save

Test it by rebooting...

sudo shutdown -r now

...after rebooting, check if it's running:

pm2 ls

Keep logs from accumulating

Install pm2-logrotate to auto-delete logs from time to time, or they'll fill up the disk.