-
Notifications
You must be signed in to change notification settings - Fork 4
Earthstar Pub on a Raspberry Pi
This was tested on a Raspberry Pi 3 Model B running a fresh install of "Raspbian 10 (buster)" in Feb 2021.
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:
- 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
- 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
- Run a pub on a hosted service like glitch.com
- 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.
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
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
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
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
npm install -g cowsay
cowsay hello
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
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-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 thewlan0
section (if it's on wifi) or theeth0
section (if it's on ethernet).
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 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 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.
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.
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.
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.
pm2
is a tool to run scripts in the background all the time, make them start on startup, and restart them if they crash.
npm install --global pm2
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
# 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 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
Install pm2-logrotate to auto-delete logs from time to time, or they'll fill up the disk.