autopush

package
v0.1.0-alpha Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2026 License: Apache-2.0 Imports: 9 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 autopush is a client of the open autopush WebSocket protocol (the one Mozilla's push service speaks), implementing webpush.Transport. The protocol is a stream of JSON text frames:

client: {"messageType":"hello","uaid":"<id or absent>","channelIDs":[...],"use_webpush":true}
server: {"messageType":"hello","uaid":"<id>","status":200}
client: {"messageType":"register","channelID":"<uuid>","key":"<VAPID public key, base64url>"}
server: {"messageType":"register","channelID":"...","status":200,"pushEndpoint":"https://..."}
client: {"messageType":"unregister","channelID":"..."}            server: {...,"status":200}
server: {"messageType":"notification","channelID":"...","version":"<id>","ttl":N,"data":"<base64url>"}
client: {"messageType":"ack","updates":[{"channelID":"...","version":"<id>","code":100}]}
either: {}    (ping, answered with {})

The connection is kept for as long as the client lives: after a failure it reconnects with exponential backoff and presents its identity (uaid) and its channel ids again, so messages stored by the service in the meantime are delivered. There is no built-in service address: the caller supplies the URL.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client implements webpush.Transport.

func New

func New(cfg Config) (*Client, error)

New starts connecting in the background.

func (*Client) Ack

func (c *Client) Ack(channelID, messageID string, code webpush.AckCode)

Ack implements webpush.Transport. An acknowledgement made while disconnected is dropped: the service delivers the message again on the next connection.

func (*Client) Close

func (c *Client) Close() error

Close stops the client.

func (*Client) Connected

func (c *Client) Connected() bool

Connected reports whether the hello handshake has completed on the current connection.

func (*Client) Register

func (c *Client) Register(ctx context.Context, channelID string, appServerKey []byte) (string, error)

Register implements webpush.Transport.

func (*Client) UAID

func (c *Client) UAID() string

UAID is the identity the service assigned.

func (*Client) Unregister

func (c *Client) Unregister(ctx context.Context, channelID string) error

Unregister implements webpush.Transport.

type Config

type Config struct {
	// URL is the service's WebSocket address (ws:// or wss://). Required.
	URL string
	// UAID is the identity stored from an earlier session ("" = a new one is issued).
	UAID    string
	Handler webpush.Handler

	PingInterval time.Duration // default 4 minutes; the connection is dropped when the service is silent for twice that
	MinBackoff   time.Duration // default 1 second
	MaxBackoff   time.Duration // default 5 minutes
	// After is the timer (default time.After); tests inject their own to make backoff and pings deterministic.
	After func(time.Duration) <-chan time.Time
	// Dial opens the WebSocket (default websocket.Dial with a 15 second handshake limit).
	Dial func(ctx context.Context, rawurl string) (*websocket.Conn, error)
	Logf func(format string, args ...any)
}

Config configures a Client.

Jump to

Keyboard shortcuts

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