How Web Push works: Push API, RFC 8030, RFC 8291 and VAPID
A web push notification can arrive when your site is not open in any tab. To make that work, the browser, a push service run by the browser vendor, and your own server pass an encrypted message along a chain defined by one W3C spec and four IETF RFCs. This post walks through how Web Push works end to end, byte by byte where it matters, with the arithmetic and a Python check against the RFC test values.
I learned most of this while building django-pushwave, a Django package for sending web push notifications. This article is about the standards, though, so it applies whatever language your server is written in.
The three parties
RFC 8030 ("Generic Event Delivery Using HTTP Push", December 2016) names three roles:
- User agent: the browser, plus your site's service worker.
- Push service: a server operated for the browser. You do not choose it; the W3C Push API says user agents "MAY limit the choice of push services available".
- Application server: your backend.
Your server never talks to the browser directly. It sends an HTTP request to the push service, which keeps a connection to the browser and delivers the message when it can. The browser then wakes your service worker, even if no page from your site is open.
Step 1: subscribing in the browser
The Push API needs an active service worker. The page asks that worker's PushManager for a subscription:
const reg = await navigator.serviceWorker.ready;
const sub = await reg.pushManager.subscribe({
userVisibleOnly: true,
applicationServerKey: VAPID_PUBLIC_KEY, // your server's P-256 public key
});
await fetch("/push/subscribe", { method: "POST", body: JSON.stringify(sub) });
Two options matter. userVisibleOnly: true is a promise that every message will produce something the user can see, usually a notification. applicationServerKey is your VAPID public key (more on that below). According to MDN, Chrome and Edge require it. MDN also notes that subscribe() should be called from a user gesture such as a click, and that Firefox enforces this from version 72.
The resulting PushSubscription serialises to JSON with three things your server must store:
endpoint: a URL on the push service. MDN calls it "a unique capability URL": knowing it is enough to send to that browser, so keep it secret.keys.p256dh: the browser's ECDH public key on P-256, as an uncompressed point (MDN, getKey).keys.auth: a 16-byte authentication secret. RFC 8291 section 3.2 says the browser MUST generate it as "a hard-to-guess sequence of 16 octets".
The W3C spec says toJSON() encodes both keys with "the URL-safe base64 encoding without padding".
Step 2: sending a message (RFC 8030)
To send, your server makes an HTTP POST to the endpoint with the encrypted body (RFC 8030 section 5). A 201 Created response means the push service accepted the message, and the RFC is clear that "This does not indicate that the message was delivered to the user agent."
Three request headers control what happens next:
TTL (required)
The TTL header is the number of seconds the push service should keep the message if the browser is offline. It is mandatory: a push service "MUST return a 400 (Bad Request)" if it is missing. The service may keep a message for less time than you asked, and says so by returning its own TTL header. With TTL: 0, the message is delivered only if the browser is reachable right now; otherwise it "expires and is never delivered".
Urgency
The optional Urgency header helps battery-powered devices decide when to wake. It defaults to normal. The RFC's illustrative table:
| Urgency | Device state | Example |
|---|---|---|
very-low | On power and Wi-Fi | Advertisements |
low | On either power or Wi-Fi | Topic updates |
normal | On neither power nor Wi-Fi | Chat or calendar message |
high | Low battery | Incoming call or time-sensitive alert |
Topic
A Topic header lets a newer message replace an undelivered older one with the same topic, so an offline phone does not wake up to ten stale "new score" messages. Topics are at most 32 characters from the URL-safe Base64 alphabet; anything else gets a 400.
Two status codes are worth handling. A push service MUST return 404 Not Found when you send to an expired subscription (section 7.3), so delete it from your database. And it may return 413 Payload Too Large, but "MUST NOT return a 413 status code in responses to an entity body that is 4096 bytes or less in size" (section 7.2). So 4,096 bytes is the only size you can count on.
Step 3: encrypting the payload (RFC 8291)
The push service carries your message, but it should not be able to read it. RFC 8291 ("Message Encryption for Web Push", November 2017) handles that, on top of the aes128gcm content coding from RFC 8188. The W3C Push API states that "The contents of a push message are encrypted", and a push event is not fired for a message that fails to decrypt.
For each message, your server:
- Generates a fresh P-256 key pair and a random 16-byte salt.
- Does ECDH between its new private key and the browser's
p256dhkey to get a shared secret. - Mixes in the browser's
authsecret with HKDF-SHA-256, using the info string"WebPush: info" || 0x00 || ua_public || as_public. - Derives a 16-byte content encryption key and a 12-byte nonce from that, with the salt (the RFC 8188 step).
- Encrypts the plaintext plus a
0x02padding delimiter with AES-128-GCM, as a single record.
The key derivation is all HMAC-SHA-256, so it can be checked with the Python standard library. I fed in the inputs from RFC 8291 Appendix A (the ECDH step needs a P-256 library, so I used the shared secret printed in the RFC):
prk_key = hmac256(auth_secret, ecdh_secret)
ikm = hmac256(prk_key, b"WebPush: info\x00" + ua_public + as_public + b"\x01")
prk = hmac256(salt, ikm)
cek = hmac256(prk, b"Content-Encoding: aes128gcm\x00\x01")[:16]
nonce = hmac256(prk, b"Content-Encoding: nonce\x00\x01")[:12]
Output:
IKM S4lYMb_L0FxCeq0WhDx813KgSYqU26kOyzWUdsXYyrg
CEK oIhVW04MRdy2XN9CiKLxTg
NONCE 4h_95klXJ5E_qnoN
header 86 bytes; max plaintext 3993
All three values match the RFC. I also encrypted the RFC's plaintext with that key and nonce using the cryptography package (AES-GCM is not in the standard library) and got exactly the ciphertext printed in Appendix A.
Web Push payload size: where 3,993 bytes comes from
The RFC 8188 header is salt (16) | rs (4) | idlen (1) | keyid (idlen). RFC 8291 puts the server's public key in keyid as a 65-byte uncompressed point, so the header is 16 + 4 + 1 + 65 = 86 bytes. Then:
- body = 86 + plaintext + 1 (delimiter) + padding + 16 (GCM tag)
- with no extra padding: body = plaintext + 103
- largest plaintext in 4,096 bytes: 4,096 − 103 = 3,993
RFC 8291 section 4 reaches the same figure: "at most, 3993 octets of plaintext". Checking it on the RFC's own example, the 41-byte string "When I grow up, I want to be a watermelon" becomes 41 + 1 + 16 = 58 bytes of ciphertext and 144 bytes of body. The example request in section 5 says Content-Length: 145; that is a known typo, reported by the RFC's author as erratum 5230, and decoding the printed body gives 144 bytes.
The fixed 103 bytes dominate small messages. A 32-byte "new message" ping is 76% overhead; a full-size message is 2.5%. RFC 8291 also notes that, without padding, message length is visible to the push service, so pad if the length itself is sensitive. And compression is not allowed: the RFC forbids other content encodings because "content encodings that compress could result in leaking of push message contents".
Step 4: identifying your server with VAPID (RFC 8292)
Encryption hides the content. RFC 8292 (VAPID, November 2017) proves who is sending. Your server keeps one long-lived P-256 signing key pair. With every request it sends:
Authorization: vapid t=<JWT>, k=<base64url public key>
The JWT is signed with ES256 (ECDSA on P-256) and carries:
aud: the origin of the push endpoint, for examplehttps://push.example.net. One token works for every endpoint on that push service.exp: the expiry, which "MUST NOT be more than 24 hours from the time of the request".sub(optional): amailto:orhttps:contact so the push service can reach you.
When the browser subscribed with applicationServerKey, the subscription is restricted to that key (section 4). The push service must then reject messages without a valid token, and the RFC suggests 401 for a missing token and 403 for an invalid one. In practice this means: keep the VAPID private key safe, and never rotate it casually, because every existing subscription is bound to the old public key.
Note the two key pairs are different. MDN puts it plainly: the application server key "IS NOT the same ECDH key that you use to encrypt the data." VAPID signs; the per-message ECDH key encrypts.
Step 5: the service worker
When the message arrives, the browser decrypts it and fires a push event in your service worker. The usual handler displays a notification with ServiceWorkerRegistration.showNotification(). MDN lists a second event, pushsubscriptionchange, for when a subscription is invalidated or about to be; handle it by sending the new subscription to your server.
MDN also notes a budget difference: Firefox limits how many push messages an app may receive unless they produce notifications, while Chrome has no such limit. Another reason to keep userVisibleOnly honest.
Browser support
From MDN's browser compatibility data for PushManager:
| Browser | Supported since | Note |
|---|---|---|
| Chrome | 42 | applicationServerKey required |
| Edge | 17 | applicationServerKey required |
| Firefox | 44 (Android 48) | User gesture required from 72 (Android 79) |
| Safari (macOS) | 16 | Notifications on macOS Ventura and later |
| Safari (iOS/iPadOS) | 16.4 | Only in web apps saved to the Home Screen |
| Android WebView | Not supported |
MDN marks the Push API as Baseline "Widely available", across browsers since March 2023. On iOS, WebKit's announcement confirms push works only after the site is added to the Home Screen, that no Apple Developer Program membership is needed, and that servers with outbound allowlists must allow *.push.apple.com.
What I would tell anyone building a sender
- Always send
TTL. Pick it per message type; a chat message is stale after minutes, a receipt maybe a day. - Use
Topicfor anything where only the latest value matters. - Treat 404 from the push service as "delete this subscription".
- Keep plaintext under 3,993 bytes. Send an ID and fetch details in the service worker if you need more.
- Cache the VAPID JWT per push service origin for less than 24 hours, instead of signing every request.
- Never log endpoints or
authsecrets.
More of my backend work is on my portfolio. If you are adding web push notifications to a Django or Python backend, email me.
References
- M. Thomson, E. Damaggio, B. Raymor, "Generic Event Delivery Using HTTP Push", RFC 8030, IETF, December 2016. https://datatracker.ietf.org/doc/html/rfc8030
- M. Thomson, "Message Encryption for Web Push", RFC 8291, IETF, November 2017. https://datatracker.ietf.org/doc/html/rfc8291
- RFC Editor, Errata for RFC 8291 (erratum 5230, example Content-Length). https://www.rfc-editor.org/errata/rfc8291
- M. Thomson, P. Beverloo, "Voluntary Application Server Identification (VAPID) for Web Push", RFC 8292, IETF, November 2017. https://datatracker.ietf.org/doc/html/rfc8292
- M. Thomson, "Encrypted Content-Encoding for HTTP", RFC 8188, IETF, June 2017. https://datatracker.ietf.org/doc/html/rfc8188
- W3C, "Push API", Working Draft. https://www.w3.org/TR/push-api/
- MDN Web Docs, "Push API". https://developer.mozilla.org/en-US/docs/Web/API/Push_API
- MDN Web Docs, "PushManager: subscribe() method". https://developer.mozilla.org/en-US/docs/Web/API/PushManager/subscribe
- MDN Web Docs, "PushSubscription: getKey() method". https://developer.mozilla.org/en-US/docs/Web/API/PushSubscription/getKey
- MDN browser-compat-data,
api/PushManager.json. https://github.com/mdn/browser-compat-data/blob/main/api/PushManager.json - WebKit blog, "Web Push for Web Apps on iOS and iPadOS", 16 February 2023. https://webkit.org/blog/13878/web-push-for-web-apps-on-ios-and-ipados/
Himal Dahal
CTO at Daleema Group. Backend developer in Kathmandu.