SubEthaConsole →

Self-hosting & gateways

There are three ways to run SubEtha pieces yourself: run the whole relay locally (for development or your own deployment), run a gateway that bridges another network to topics, or write your own gateway with an SDK.

Run the relay locally

The relay is one Cloudflare Worker with Durable Objects, D1, KV, R2, and a Queue. wrangler dev simulates all of them on your machine.

#!/usr/bin/env bash
# Run the relay locally and get a funded account and admin token.
set -euo pipefail

# In one terminal (from the repository root):
#   pnpm install && pnpm --filter @subetha/sdk build && pnpm --filter @subetha/site build
#   pnpm --filter @subetha/api exec wrangler d1 migrations apply DB --local
#   pnpm --filter @subetha/api dev          # http://localhost:8787

# In another:
export SUBETHA_URL=http://localhost:8787
SUBETHA_TOKEN=$(curl -sS -X POST "$SUBETHA_URL/v1/dev/bootstrap" \
  -d '{"secret":"local-dev-only","handle":"me","credit_usd":5}' | jq -r .token)
export SUBETHA_TOKEN

curl -sS -H "Authorization: Bearer $SUBETHA_TOKEN" -d "hello from localhost" "$SUBETHA_URL/@me/test"
curl -sS -H "Authorization: Bearer $SUBETHA_TOKEN" "$SUBETHA_URL/@me/test/json?poll=1"

POST /v1/dev/bootstrap exists only when ENVIRONMENT=local and DEV_BOOTSTRAP_SECRET is set (both are, in the default local config in workers/api/wrangler.jsonc). Provider integrations (Stripe, Twilio, Slack, push, email) stay disabled until you set their secrets in workers/api/.dev.vars; see infra/secrets.md.

Deploy your own

infra/README.md has the one-time Cloudflare setup (D1, KV, R2, Queues, rate limits, custom domains) and the GitHub Actions deploy workflow (staging on every push to main, production on v* tags). Point SDKs and clients at your deployment with baseUrl / SUBETHA_URL.

To deliver to iOS through APNs, also run the APNs relay: APNs needs HTTP/2, which a Worker can't be relied on to speak. It's one dependency-free Node process (or Docker image) with no Apple credentials; set APNS_ENDPOINT and APNS_RELAY_SECRET to point the Worker at it.

Reticulum gateway

gateways/reticulum bridges Reticulum/LXMF meshes to topics. Each mapped topic gets its own LXMF address, so anyone on the mesh (Sideband, NomadNet, MeshChat) can publish to it and /subscribe to it with no IP connectivity at all.

pip install -e sdk/python -e "gateways/reticulum[rns]"
cp gateways/reticulum/gateway.example.toml gateway.toml   # list your topics
export SUBETHA_TOKEN=se_…   # publish + subscribe on those topics, with a spend cap
subetha-reticulum --config gateway.toml

It runs anywhere Reticulum does (a Raspberry Pi with an RNode, a home server, a VPS) and needs no special access: it uses only the public API. Several gateways can serve the same topic, and they won't echo each other's messages. The gateway's token pays for what mesh users publish, so scope it and cap it. The hosted gateway also accepts relay-pushed deliveries for lxmf endpoints on POST /deliver (HMAC-signed with LXMF_GATEWAY_SECRET). Docker and Cloudflare Containers notes are in the gateway's README.

Bring your own gateway

Anything that can subscribe and publish is a gateway. The pattern: subscribe to the topic and forward to your transport; read from your transport and publish to the topic; remember the ids you published so you don't echo them back.

"""A minimal bring-your-own gateway: bridge a topic to any local transport.

Anything that can subscribe and publish over the public API is a gateway. This one bridges a
topic to lines on a serial port (a LoRa modem, an Arduino…). Replace send_local/read_local with
your transport. gateways/reticulum is a complete example of the same pattern.
"""
import os
import threading

from subetha import Client

TOPIC = os.environ.get("TOPIC", "@me/mesh")
client = Client(os.environ.get("SUBETHA_URL", "https://subetha.us"), token=os.environ["SUBETHA_TOKEN"])
seen: set[str] = set()  # ids we published, so we don't echo them back


def send_local(text: str) -> None:
    print(f"-> local: {text}")  # e.g. serial.write(text.encode() + b"\n")


def read_local():
    while True:  # e.g. yield serial.readline().decode().strip()
        yield input()


def relay_to_local() -> None:
    for msg in client.subscribe(TOPIC):  # reconnects and resumes from the last id
        if msg["id"] in seen:
            continue
        send_local(msg.get("message", ""))


def local_to_relay() -> None:
    for line in read_local():
        if line:
            sent = client.publish(TOPIC, line, tags=["gateway"], route={"deny": ["sms", "voice"]})
            seen.add(sent["id"])


threading.Thread(target=relay_to_local, daemon=True).start()
local_to_relay()

Tips:

Edit this page · Markdown