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 a single hard-coded client.
For most projects 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 which presents how to
setup an axum
web server in combination with this library to expose a
simple HTTP API for sending web-push notifications.
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 by calling `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>>, Box<dyn std::error::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");
Ok(builder.build(content)?)
}
Re-exports§
pub use jwt_simple;
pub use p256;
Structs§
- WebPush
Builder - Reusable builder for HTTP push requests
Enums§
- Error
- Error type for HTTP push failure modes
Functions§
- decrypt
- Lower-level decryption used for HTTP push request content
- encrypt
- Lower-level encryption used for HTTP push request content
Type Aliases§
- Auth
- HTTP push authentication secret