Crate web_push_native

source ·
Expand description

This crate implements “Generic Event Delivery Using Http Push” (web-push) according to RFC8030.

Example

This example shows how to use the WebPushBuilder to create a HTTP push request to one, hard-coded client.

In most cases, you will need to implement some form of state management to send messages to all of your clients. You are expected to create one WebPushBuilder for each client you want to send messages to, but can reuse the same builder for multiple push requests to the same client.

Please see the /example directory on GitHub for a more fully-featured example.

use base64ct::{Base64UrlUnpadded, Encoding};
use web_push_native::{
    jwt_simple::algorithms::ES256KeyPair, p256::PublicKey, Auth, Error, WebPushBuilder,
};

// Placeholders for variables provided by individual clients. In most cases,
// these will be retrieved in-browser using `pushManager.subscribe` on a service
// worker registration object.
const ENDPOINT: &str = "";
const P256DH: &str = "";
const AUTH: &str = "";

// Placeholder for your private VAPID key. Keep this private and out of your
// source tree in real projects!
const VAPID: &str = "";

async fn push(content: Vec<u8>) -> Result<http::Request<Vec<u8>>, Error> {
    let key_pair = ES256KeyPair::from_bytes(&Base64UrlUnpadded::decode_vec(VAPID)?)?;
    let builder = WebPushBuilder::new(
        ENDPOINT.parse()?,
        PublicKey::from_sec1_bytes(&Base64UrlUnpadded::decode_vec(P256DH)?)?,
        Auth::clone_from_slice(&Base64UrlUnpadded::decode_vec(AUTH)?),
    )
    .with_vapid(&key_pair, "mailto:john.doe@example.com");

    builder.build(content)
}

Re-exports

pub use jwt_simple;
pub use p256;

Structs

Reusable builder for HTTP push requests

Functions

Lower-level decryption used for HTTP push request content
Lower-level encryption used for HTTP push request content

Type Definitions

HTTP push authentication secret
Opaque error type for HTTP push failure modes