← All posts

How Web Push works: Push API, RFC 8030, RFC 8291 and VAPID

28 Nov 2026 · 9 min read

How Web Push reaches a closed browser tab: Push API, service workers, RFC 8030, RFC 8291 and VAPID, end to end

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.

Sequence diagram with three lanes: browser and service worker, push service, application server. 1: browser subscribes and gets an endpoint URL. 2: browser posts the subscription with p256dh and auth keys to the application server. 3: server encrypts and signs a VAPID token. 4: server POSTs to the endpoint with TTL, Urgency and Topic, and gets 201 Created. 5: push service stores the message until the device is online or TTL ends. 6: it delivers the encrypted body. 7: the browser decrypts and the service worker shows a notification.

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:

UrgencyDevice stateExample
very-lowOn power and Wi-FiAdvertisements
lowOn either power or Wi-FiTopic updates
normalOn neither power nor Wi-FiChat or calendar message
highLow batteryIncoming 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:

  1. Generates a fresh P-256 key pair and a random 16-byte salt.
  2. Does ECDH between its new private key and the browser's p256dh key to get a shared secret.
  3. Mixes in the browser's auth secret with HKDF-SHA-256, using the info string "WebPush: info" || 0x00 || ua_public || as_public.
  4. Derives a 16-byte content encryption key and a 12-byte nonce from that, with the salt (the RFC 8188 step).
  5. Encrypts the plaintext plus a 0x02 padding 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

Byte layout of an encrypted push body: salt 16 bytes, record size 4 bytes, idlen 1 byte, keyid 65 bytes (the server's public key), then the ciphertext of plaintext plus 0x02 plus padding, then a 16-byte GCM tag. The 86-byte header is sent in the clear. The RFC 8291 example: 41 bytes of plaintext becomes 58 bytes of ciphertext and a 144-byte body.

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.

Horizontal stacked bar chart of request body size for five plaintext sizes. 32 bytes becomes 135 bytes, 76.3% overhead. 256 becomes 359, 28.7%. 1,024 becomes 1,127, 9.1%. 2,048 becomes 2,151, 4.8%. 3,993 becomes exactly 4,096, 2.5%.

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 example https://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): a mailto: or https: 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:

BrowserSupported sinceNote
Chrome42applicationServerKey required
Edge17applicationServerKey required
Firefox44 (Android 48)User gesture required from 72 (Android 79)
Safari (macOS)16Notifications on macOS Ventura and later
Safari (iOS/iPadOS)16.4Only in web apps saved to the Home Screen
Android WebViewNot 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 Topic for 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 auth secrets.

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

  1. M. Thomson, E. Damaggio, B. Raymor, "Generic Event Delivery Using HTTP Push", RFC 8030, IETF, December 2016. https://datatracker.ietf.org/doc/html/rfc8030
  2. M. Thomson, "Message Encryption for Web Push", RFC 8291, IETF, November 2017. https://datatracker.ietf.org/doc/html/rfc8291
  3. RFC Editor, Errata for RFC 8291 (erratum 5230, example Content-Length). https://www.rfc-editor.org/errata/rfc8291
  4. M. Thomson, P. Beverloo, "Voluntary Application Server Identification (VAPID) for Web Push", RFC 8292, IETF, November 2017. https://datatracker.ietf.org/doc/html/rfc8292
  5. M. Thomson, "Encrypted Content-Encoding for HTTP", RFC 8188, IETF, June 2017. https://datatracker.ietf.org/doc/html/rfc8188
  6. W3C, "Push API", Working Draft. https://www.w3.org/TR/push-api/
  7. MDN Web Docs, "Push API". https://developer.mozilla.org/en-US/docs/Web/API/Push_API
  8. MDN Web Docs, "PushManager: subscribe() method". https://developer.mozilla.org/en-US/docs/Web/API/PushManager/subscribe
  9. MDN Web Docs, "PushSubscription: getKey() method". https://developer.mozilla.org/en-US/docs/Web/API/PushSubscription/getKey
  10. MDN browser-compat-data, api/PushManager.json. https://github.com/mdn/browser-compat-data/blob/main/api/PushManager.json
  11. 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/
← Building an offline-first note-taking P…
Written by

Himal Dahal

CTO at Daleema Group. Backend developer in Kathmandu.

Get in touch