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 ¶
- type Client
- func (c *Client) Ack(channelID, messageID string, code webpush.AckCode)
- func (c *Client) Close() error
- func (c *Client) Connected() bool
- func (c *Client) Register(ctx context.Context, channelID string, appServerKey []byte) (string, error)
- func (c *Client) UAID() string
- func (c *Client) Unregister(ctx context.Context, channelID string) error
- type Config
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 (*Client) Ack ¶
Ack implements webpush.Transport. An acknowledgement made while disconnected is dropped: the service delivers the message again on the next connection.
func (*Client) Connected ¶
Connected reports whether the hello handshake has completed on the current connection.
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.