push

package
v0.9.13 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 2, 2026 License: MIT Imports: 30 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package push sends notifications to people's phones and computers with standard Web Push: Safari on macOS, Home Screen web apps on iOS and iPadOS, and Chromium and Firefox, through one pipeline and with no Apple developer account.

A device subscribes once, on the portal, and then gets Outpost's own notifications and those of every app its user can open and hasn't muted. Apps don't see subscriptions. Each is started with OUTPOST_PUSH_URL, a loopback endpoint this package serves, and OUTPOST_PUSH_TOKEN, its own, and names who a notification is for; Outpost decides which devices that means. Tapping one opens the portal, which sends the device on to the app, so a notification's link is always the subscription's own origin, as Declarative Web Push requires. See docs/design/push.md.

Index

Constants

View Source
const (
	EnvURL   = "OUTPOST_PUSH_URL"
	EnvToken = "OUTPOST_PUSH_TOKEN"
)

Environment variables every app is started with.

View Source
const DefaultTTL = 24 * 60 * 60

DefaultTTL is how long a push service holds a notification for a device that's off, when the sender doesn't say.

View Source
const MaxPayload = recordSize - headerLen - 1 - 16

MaxPayload is the largest plaintext that still fits one 4096-byte record once the header, the delimiter and the GCM tag are added, which is the most a push service has to take (RFC 8030 §7.2).

View Source
const Outpost = ""

Outpost is the sender name of Outpost's own notifications. No app can be called it: a service's name is never empty.

Variables

View Source
var (
	ErrNotReady = errors.New("notifications aren't available: the login gateway isn't running")
	ErrLimit    = fmt.Errorf("at most %d notifications an hour from one app", sendLimit)
)

Errors Send returns besides a bad notification.

Functions

func IsBadRequest

func IsBadRequest(err error) bool

IsBadRequest reports whether err is a fault in the notification itself.

func ValidSubject

func ValidSubject(s string) error

ValidSubject checks a VAPID contact: Apple refuses a request whose sub isn't a mailto: or https: URI.

Types

type Device

type Device struct {
	ID          string
	SignIn      string
	UserAgent   string
	Platform    string
	CreatedAt   time.Time
	LastSuccess time.Time
	Failing     bool
}

Device is a subscription as its owner sees it: no keys, and no endpoint, which is as good as one.

type Gate

type Gate interface {
	// MayReceive reports whether username may be sent service's
	// notifications; Outpost is Outpost's own.
	MayReceive(service, username string) bool
	// SignIns lists username's live sign-ins by ID; an error means none
	// can be checked now, not that there are none.
	SignIns(username string) (map[string]bool, error)
	// Navigate is the absolute URL, on the portal, that a notification
	// from service about path opens.
	Navigate(service, path string) string
}

Gate is what Send needs from the gateway: who may receive what, whether a device is still signed in, and where tapping a notification goes.

type Notification

type Notification struct {
	// Title is required; Body is the line under it.
	Title string `json:"title"`
	Body  string `json:"body,omitempty"`
	// Path is where on the sender tapping it goes: a path on the app's own
	// host for an app, on the portal for Outpost's own. Unset is "/".
	Path string `json:"path,omitempty"`
	// Tag replaces an earlier notification with the same tag on a device,
	// rather than stacking another.
	Tag string `json:"tag,omitempty"`
	// Badge, when set, is the number on the Home Screen or Dock icon; 0
	// clears it.
	Badge *int `json:"badge,omitempty"`
	// Silent asks the device not to make a sound.
	Silent bool `json:"silent,omitempty"`
	// Urgency is very-low, low, normal (the default) or high: how far a
	// device on low battery goes to deliver it.
	Urgency string `json:"urgency,omitempty"`
	// TTL is how long, in seconds, the push service holds it for a device
	// that is off; 0 is the default, a day.
	TTL int `json:"ttl,omitempty"`
	// Topic replaces one still waiting at the push service with the same
	// topic, so a device that was off gets only the latest.
	Topic string `json:"topic,omitempty"`
}

Notification is one message for people's devices.

type Options

type Options struct {
	// Path is push.db.
	Path string
	// Listen is the loopback address the endpoint serves on, which apps are
	// told as OUTPOST_PUSH_URL.
	Listen string
	// Subject is the VAPID contact a push service can reach the operator
	// at: a mailto: or https: URI.
	Subject string
	// Logf receives a line per subscription change and per failed delivery.
	Logf func(format string, v ...any)
}

Options configures a Service.

type Service

type Service struct {
	// contains filtered or unexported fields
}

Service holds the subscriptions and sends to them. It is safe for concurrent use.

func Open

func Open(opts Options) (*Service, error)

Open opens push.db, making the VAPID key the first time; Serve starts the endpoint and Run delivers.

func (*Service) Close

func (s *Service) Close() error

Close closes push.db.

func (*Service) Devices

func (s *Service) Devices(username string) ([]Device, error)

Devices lists username's subscriptions, oldest first.

func (*Service) Forget

func (s *Service) Forget(username, id string) (bool, error)

Forget removes one of username's subscriptions; it reports whether there was one.

func (*Service) Muted

func (s *Service) Muted(username string) (map[string]bool, error)

Muted lists the senders username has turned off; Outpost is Outpost's.

func (*Service) PublicKey

func (s *Service) PublicKey() string

PublicKey is the VAPID public key browsers subscribe with.

func (*Service) Resolve

func (s *Service) Resolve(service string) (map[string]string, string)

Resolve gives an app its OUTPOST_PUSH_URL and OUTPOST_PUSH_TOKEN. The token is an HMAC of its name, so it is the same on every start and needs no revision of its own.

func (*Service) Run

func (s *Service) Run(ctx context.Context)

Run delivers queued notifications until ctx ends. Deliveries still queued then are dropped: a notification is for now, and a restart is brief.

func (*Service) Send

func (s *Service) Send(from string, to []string, n Notification) (int, error)

Send queues n from from for to's devices, or, with no one named, for everyone with a device who may receive from's notifications. Someone who can't open the app, has muted it, or has no device gets nothing, and that isn't an error. It reports how many devices it was queued for; delivery happens in Run.

func (*Service) Serve

func (s *Service) Serve(ctx context.Context, ln net.Listener) error

Serve answers apps on ln until ctx ends.

func (*Service) ServeHTTP

func (s *Service) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP is the endpoint apps call.

func (*Service) SetGate

func (s *Service) SetGate(g Gate)

SetGate tells the Service who may receive what, once the gateway exists. Until then nothing is sent.

func (*Service) SetMuted

func (s *Service) SetMuted(username, service string, muted bool) error

SetMuted turns service's notifications off or on for username.

func (*Service) Status

func (s *Service) Status() Stats

Status counts deliveries since the daemon started.

func (*Service) Subscribe

func (s *Service) Subscribe(username, signIn, endpoint, p256dh, auth, userAgent string) error

Subscribe keeps a browser's subscription for username, made from the sign-in signIn. It replaces one with the same endpoint.

func (*Service) URL

func (s *Service) URL() string

URL is what apps are told as OUTPOST_PUSH_URL.

func (*Service) Unsubscribe

func (s *Service) Unsubscribe(username, endpoint string) error

Unsubscribe forgets username's subscription at endpoint, as a browser turning notifications off asks.

type Stats

type Stats struct {
	Delivered int `json:"delivered"`
	// Gone are subscriptions the push service said no longer exist, which
	// are forgotten.
	Gone int `json:"gone"`
	// Failed are deliveries given up on.
	Failed int `json:"failed"`
	// Dropped are deliveries that didn't fit the queue.
	Dropped int `json:"dropped"`
}

Stats counts deliveries since the daemon started.

type Subscription

type Subscription struct {
	ID       string
	Username string
	// SignIn is the sign-in it was made from; when that ends, so does this.
	SignIn    string
	Endpoint  string
	P256dh    string
	Auth      string
	UserAgent string
	Platform  string
	CreatedAt time.Time
	// LastSuccess is the last push the service took for it, zero if none.
	LastSuccess  time.Time
	FailureCount int
}

Subscription is one device's subscription, as a browser made it. Keys are credentials: anyone holding them and the endpoint can send to the device, so they are never logged or shown.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL