Home JavaScript PushEvent JavaScript PushEvent Definition
Definition and Usage A PushEvent is fired on a service worker when a push message arrives from your server. Listen with self.addEventListener("push", handler) inside the worker.
The payload is on event.data. Call event.data.json() or text() to read it. Use event.waitUntil(promise) so the worker stays alive while you show a notification.
This page teaches what PushEvent is. Constructor and property guides live under this section.
Runs in a service worker, not the page. Page scripts do not receive PushEvent. Register a service worker and handle push there.
Syntax
JavaScript Syntax Handling a push in a service worker:
self.addEventListener("push", function (event) {
const payload = event.data ? event.data.json() : {};
event.waitUntil(
self.registration.showNotification(payload.title || "Update")
);
}); Reference
Core Ideas Ideas every PushEvent tutorial builds on:
Idea Example Remember Where service workerNot the window page Listen addEventListener("push")On self in the worker data event.dataPayload or null json() event.data.json()Parse JSON body waitUntil event.waitUntil(p)Keep worker alive Notify showNotification(...)Visible user alert
Code
JavaScript Examples Three short programs that show how PushEvent works.
1. type 2. data 3. waitUntil Example 1 — PushEvent type Constructed PushEvents use the type push by default in workers; tests can build one.
const event = new PushEvent("push");
console.log(event.type); How It Works new PushEvent("push") builds a test event.type is "push".Real events arrive from the push service. Example 2 — Read JSON data When a body is present, data exposes json() and text().
// inside push handler:
const data = event.data ? event.data.json() : null;
console.log(data && data.title); How It Works event.data may be null for empty pushes.json() parses a JSON payload.Use the fields to build your notification. Example 3 — Keep the worker alive waitUntil accepts a promise for async work.
event.waitUntil(
self.registration.showNotification("Saved")
); How It Works showNotification returns a promise.waitUntil extends the event lifetime.Without it, the worker may sleep too soon. Continue
Next: PushEvent Guides Continue with the constructor and the data property.
Look up Why Constructor Create PushEvent objects dataAccess the push payload waitUntil()Extend event lifetime PushManager Subscribe from the page
Full lists: PushEvent constructor .
Frequently Asked Questions What is a PushEvent? A PushEvent is delivered to a service worker when a push message arrives. It may include a data payload and supports waitUntil for async handling.
Can my page script listen for push? No. The push event fires on the service worker. The page may react later through messages or notification clicks.
What if event.data is null? Some pushes are empty signals. Guard with if (event.data) before calling json() or text().
Why use waitUntil? Service workers can stop quickly. waitUntil keeps them alive until your notification or fetch promise settles.
Where should I go next? Open the PushEvent constructor, then PushManager for subscriptions from the page.
Open the PushEvent constructor Then read the data property and pair with PushManager subscriptions.
PushEvent constructor → CODETOFUN NEWS
Stay Ahead in Tech Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
12 people found this page helpful
Helpful Share Copy link Suggestion