How web push works
Web push has three parties: your site's service worker, the browser's push service and your server. The full flow from permission prompt to notification.
Updated
Web push lets a website show a notification when the site is not open. It works through three parties: a service worker your site installs in the browser, a push service run by the browser vendor, and your own server. Your server never talks to the browser directly. It sends an encrypted message to the push service, which delivers it to the browser, which wakes your service worker.
The pieces
| Piece | Job |
|---|---|
| Service worker | A script that runs in the background and receives the push event |
| Push API | Creates a subscription (PushManager.subscribe()) |
| Notifications API | Asks permission and displays the notification |
| Push service | The browser vendor's delivery server (Google's for Chrome, Mozilla's for Firefox, Apple's for Safari, Microsoft's for Edge) |
| Your server | Stores subscriptions and sends messages using the Web Push protocol |
Service workers and push require HTTPS. localhost is treated as secure for development.
The flow
- Register a service worker.
- Ask for permission with
Notification.requestPermission(). Do this in response to a click or tap; Safari and Firefox require a user gesture. - Subscribe. Call
pushManager.subscribe()with your VAPID public key. The browser contacts its push service and returns aPushSubscription. - Send the subscription to your server and store it.
- Send a message. Your server encrypts a payload for that subscription, signs the request with your VAPID private key, and POSTs it to the subscription's endpoint.
- Receive it. The push service delivers the message, the browser starts your service worker and fires a
pushevent, and your code shows a notification.
What a subscription looks like
JSON.stringify(subscription) gives you something like this:
{
"endpoint": "https://push-service.example/send/abc123",
"expirationTime": null,
"keys": {
"p256dh": "<the browser's public key>",
"auth": "<an authentication secret>"
}
}
The endpoint is a unique URL at the push service. Treat it as a secret: anyone with the endpoint and your VAPID private key can push to that browser. The keys are used to encrypt the payload so the push service cannot read it.
Subscribing in the page
const registration = await navigator.serviceWorker.register('/sw.js');
const permission = await Notification.requestPermission();
if (permission === 'granted') {
const subscription = await registration.pushManager.subscribe({
userVisibleOnly: true,
applicationServerKey: urlBase64ToUint8Array(VAPID_PUBLIC_KEY),
});
await fetch('/api/push/subscribe', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(subscription),
});
}
urlBase64ToUint8Array is a small helper that decodes the key; it is shown in VAPID keys explained. userVisibleOnly: true is a promise that every push will show a notification. Chrome and Safari require it.
Receiving in the service worker
self.addEventListener('push', (event) => {
const data = event.data ? event.data.json() : {};
event.waitUntil(
self.registration.showNotification(data.title ?? 'New message', {
body: data.body,
data: { url: data.url ?? '/' },
})
);
});
self.addEventListener('notificationclick', (event) => {
event.notification.close();
event.waitUntil(clients.openWindow(event.notification.data.url));
});
event.waitUntil() keeps the service worker alive until the notification is shown.
Things that catch people out
- Payload size. Keep payloads small. Push services are only required to accept about 4 KB.
- Subscriptions expire. When a push service answers
404or410, the subscription is gone. Delete it from your database. - Permission is per origin and, once denied, can only be changed by the user in browser settings. Ask at a moment when the value is obvious.
Next: VAPID keys explained, Safari push setup, or web push in Next.js.