JavaScript PushEvent

Beginner
8 min read
Updated: Nov 2026
3 examples
Push messages

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.

JavaScript Syntax

Handling a push in a service worker:

JavaScript
self.addEventListener("push", function (event) {
  const payload = event.data ? event.data.json() : {};
  event.waitUntil(
    self.registration.showNotification(payload.title || "Update")
  );
});

Core Ideas

Ideas every PushEvent tutorial builds on:

IdeaExampleRemember
Whereservice workerNot the window page
ListenaddEventListener("push")On self in the worker
dataevent.dataPayload or null
json()event.data.json()Parse JSON body
waitUntilevent.waitUntil(p)Keep worker alive
NotifyshowNotification(...)Visible user alert

JavaScript Examples

Three short programs that show how PushEvent works.

Example 1 — PushEvent type

Constructed PushEvents use the type push by default in workers; tests can build one.

JavaScript
const event = new PushEvent("push");
console.log(event.type);

How It Works

  1. new PushEvent("push") builds a test event.
  2. type is "push".
  3. Real events arrive from the push service.

Example 2 — Read JSON data

When a body is present, data exposes json() and text().

JavaScript
// inside push handler:
const data = event.data ? event.data.json() : null;
console.log(data && data.title);

How It Works

  1. event.data may be null for empty pushes.
  2. json() parses a JSON payload.
  3. Use the fields to build your notification.

Example 3 — Keep the worker alive

waitUntil accepts a promise for async work.

JavaScript
event.waitUntil(
  self.registration.showNotification("Saved")
);

How It Works

  1. showNotification returns a promise.
  2. waitUntil extends the event lifetime.
  3. Without it, the worker may sleep too soon.

Next: PushEvent Guides

Continue with the constructor and the data property.

Look upWhy
ConstructorCreate PushEvent objects
dataAccess the push payload
waitUntil()Extend event lifetime
PushManagerSubscribe from the page

Full lists: PushEvent constructor.

Frequently Asked Questions

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.
No. The push event fires on the service worker. The page may react later through messages or notification clicks.
Some pushes are empty signals. Guard with if (event.data) before calling json() or text().
Service workers can stop quickly. waitUntil keeps them alive until your notification or fetch promise settles.
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.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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