Help us shape the future of Go! Take the 2026 Go Developer Survey

webpush

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2026 License: MIT Imports: 24 Imported by: 0

Documentation

Overview

Package webpush sends RFC 8030 Web Push messages to the browser push services: RFC 8291 payload encryption, RFC 8292 application server identification, and nothing else. It knows about a VAPID key pair, a subscription, and an HTTP client, so it can be lifted out of the server unchanged.

Index

Constants

View Source
const (
	// MaxBodyRunes bounds the message text a notification carries. Lock
	// screens truncate long bodies anyway, and the payload has to stay inside
	// one encrypted record.
	MaxBodyRunes = 240
)

Variables

View Source
var ErrSubscriptionGone = errors.New("push subscription is gone")

ErrSubscriptionGone reports that the push service has permanently rejected the subscription. The caller deletes the stored row; no other failure means the device is gone.

Functions

func DecodeKey

func DecodeKey(value string) ([]byte, error)

DecodeKey accepts the base64url a browser, an operator, or a config file is likely to carry: with or without padding, and with the standard alphabet.

func GenerateKeys

func GenerateKeys() (publicKey, privateKey string, err error)

GenerateKeys returns a fresh base64url VAPID key pair, public key first. The public key is the uncompressed P-256 point the browser passes to pushManager.subscribe; the private key is the raw scalar.

func KeyFingerprint

func KeyFingerprint(publicKey string) string

KeyFingerprint names a VAPID public key in a log line: the first twelve hex digits of the SHA-256 of its bytes, so every spelling of one key agrees. The key is public; the fingerprint keeps the line short and changes when the operator rotates the pair.

func NormalizeSubject

func NormalizeSubject(subject string) (string, error)

NormalizeSubject returns the RFC 8292 contact for the application server. A value that already carries its scheme is kept byte for byte: push services reject a doubled "mailto:" prefix, and Apple rejects every push that carries one.

func RelayHost

func RelayHost(endpoint string) string

RelayHost reports the push service host for a subscription endpoint. It is the only part of an endpoint that is safe to log: the path carries the device's delivery secret.

func ValidateKeys

func ValidateKeys(publicKey, privateKey string) error

ValidateKeys rejects malformed or mismatched VAPID identities before devices register.

func ValidateSubscriptionKeys

func ValidateSubscriptionKeys(p256dh, auth string) error

ValidateSubscriptionKeys rejects client keys that cannot produce a payload: a p256dh that is not a point on P-256, or an auth secret of the wrong length. Registration calls it so a device can never be stored in a state the delivery worker would choke on.

Types

type Message

type Message struct {
	UserID string `json:"user_id"`
	Title  string `json:"title"`
	Body   string `json:"body"`
	Tag    string `json:"tag"`
	URL    string `json:"url"`
}

Message is the payload a service worker receives.

type RelayError

type RelayError struct {
	Host       string
	Status     int
	RetryAfter time.Duration
}

RelayError is a push service refusal that says nothing about whether the subscription is still good: rate limits, outages, and anything else the service reports with a status code. The caller backs off.

func (*RelayError) Error

func (e *RelayError) Error() string

type Sender

type Sender struct {
	PublicKey  string
	PrivateKey string
	Subject    string
	Client     *http.Client
}

Sender holds the application server identity shared by every push.

func (*Sender) Send

func (s *Sender) Send(ctx context.Context, subscription Subscription, message Message) error

Send encrypts one message for a subscription and posts it to the push service.

type Subscription

type Subscription struct {
	Endpoint string
	P256dh   string
	Auth     string
}

Subscription is the durable half of a browser PushSubscription.

Jump to

Keyboard shortcuts

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