JavaScript PushEvent Constructor

Beginner
⏱️ 11 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Baseline Widely available
Service worker only

What You’ll Learn

The PushEvent() constructor creates a PushEvent in a service worker—the object behind the push and pushsubscriptionchange events in the Push API. Learn type, the optional data payload, PushMessageData, secure-context rules, and how it extends ExtendableEvent—with five examples and try-it labs.

01

Create

new PushEvent()

02

Types

push / subscription

03

Data

PushMessageData

04

Context

Service worker

05

Extends

ExtendableEvent

06

Status

Baseline Mar 2023

Introduction

Web push lets an application server send messages to a subscribed browser even when your site is closed. When a push arrives, the browser wakes the service worker and fires a push event. The handler receives a PushEvent with optional payload bytes in event.data.

The PushEvent() constructor lets you create that event object yourself—mainly for tests, tooling, or learning how data is wrapped as PushMessageData. MDN notes it is exposed only in a service worker context.

💡
Beginner tip

You will not find PushEvent on window in DevTools on a normal page. Open your service worker script or use the try-it labs, which register a short-lived worker to run the constructor.

Understanding the PushEvent() Constructor

Pass an event type string and an optional options object. The constructor returns a new PushEvent instance.

  • type"push" or "pushsubscriptionchange" (case-sensitive).
  • data — optional payload; becomes event.data as PushMessageData.
  • InheritsExtendableEvent options such as bubbles and cancelable.
  • Secure context — Push API features require HTTPS (or localhost).
  • Production use — real pushes are delivered by the browser; you listen with self.addEventListener("push", ...).

📝 Syntax

JavaScript
new PushEvent(type)
new PushEvent(type, options)

Parameters

  • type — A string with the name of the event.
  • options (optional) — In addition to ExtendableEvent fields, may include data.

Option fields (MDN)

OptionMeaning
dataPayload bytes; wrapped as PushMessageData on event.data
bubblesFrom ExtendableEvent / Event (default false)
cancelableWhether the event can be cancelled (default false)

Return value

A new PushEvent object.

⚡ Quick Reference

GoalCode
MDN text payloadnew PushEvent("push", { data: "Some sample text" })
Read textevent.data.text()
Read JSONevent.data.json()
Subscription changenew PushEvent("pushsubscriptionchange")
Listen for real pushself.addEventListener("push", (e) => { ... })
MDN statusBaseline Widely available (since March 2023)

🔍 At a Glance

Four facts to remember about new PushEvent().

Returns
PushEvent

Push API event

Context
SW only

Service worker

Payload
.data

PushMessageData

Baseline
Mar 2023

Widely available

Examples Gallery

Examples follow MDN PushEvent(). Run them in a service worker. Use View Output or Try It Yourself.

📚 Getting Started

Create PushEvent objects and read data.

Example 1 — MDN: Text data Option

Official MDN sample—constructor sets PushMessageData.

JavaScript
const dataInit = {
  data: "Some sample text",
};

const myPushEvent = new PushEvent("push", dataInit);

myPushEvent.data.text(); // "Some sample text"
Try It Yourself

How It Works

The data option is stored on event.data as PushMessageData.

Example 2 — Basic push Type

Create a push event with only the type string.

JavaScript
const evt = new PushEvent("push");

console.log(evt.type);              // "push"
console.log(evt instanceof PushEvent); // true
console.log(evt.data);              // null (no data option)
Try It Yourself

How It Works

Without data, event.data is null—common for silent push pings.

📈 Subscription & Real Handlers

Other event types and production patterns.

Example 3 — pushsubscriptionchange Type

MDN lists this type for subscription lifecycle changes.

JavaScript
const evt = new PushEvent("pushsubscriptionchange");

console.log(evt.type); // "pushsubscriptionchange"
Try It Yourself

How It Works

Listen with self.addEventListener("pushsubscriptionchange", ...) to refresh subscriptions.

Example 4 — JSON Payload with data.json()

Servers often send JSON strings; parse them in the handler.

JavaScript
const payload = JSON.stringify({
  title: "New message",
  body: "You have mail",
});

const evt = new PushEvent("push", { data: payload });
const data = evt.data.json();

console.log(data.title); // "New message"
console.log(data.body);  // "You have mail"
Try It Yourself

How It Works

PushMessageData.json() parses UTF-8 JSON from the payload bytes.

Example 5 — Production push Handler Pattern (MDN)

How a real service worker reads push data and shows a notification.

JavaScript
self.addEventListener("push", (event) => {
  if (!(self.Notification && self.Notification.permission === "granted")) {
    return;
  }

  const data = event.data?.json() ?? {};
  const title = data.title || "Something Has Happened";
  const message =
    data.message || "Here's something you might want to check out.";

  event.waitUntil(
    self.registration.showNotification(title, {
      body: message,
      tag: "simple-push-demo-notification",
    })
  );
});
Try It Yourself

How It Works

Real pushes arrive via the browser; the handler uses event.waitUntil from ExtendableEvent to keep the worker alive while showing a notification.

🚀 Common Use Cases

  • Unit tests that simulate push payloads in a service worker.
  • Learning how PushMessageData wraps server bytes.
  • Debugging subscription flows with pushsubscriptionchange.
  • Building PWAs that show notifications from push data.
  • Teaching the difference between constructing events vs receiving real pushes.

🔧 How It Works

1

Service worker runs

PushEvent is only available in the worker global scope.

Context
2

Call constructor

new PushEvent("push", { data }) with optional payload.

Create
3

Wrap data

Bytes become PushMessageData on event.data.

Payload
4

Read or handle

Use text(), json(), or pass to your push handler logic.

📝 Notes

  • Baseline Widely available (MDN, since March 2023).
  • Not Deprecated, Experimental, or Non-standard — no status banner required.
  • Service worker only — not on the main thread.
  • Secure context — HTTPS or localhost for Push API features.
  • Extends ExtendableEvent — real handlers often call event.waitUntil(promise).
  • Related learning: navigator.serviceWorker, Worker(), Event(), JavaScript hub.

Universal Browser Support

The PushEvent() constructor is Baseline Widely available on MDN (since March 2023). It requires a secure context and is exposed in service workers. Logos use the shared browser-image-sprite.png sprite from this project.

Baseline · Widely available

PushEvent() constructor

Create PushEvent objects in service workers for the Push API.

Universal Widely available
Google Chrome Full support · Desktop & Mobile
Full support
Mozilla Firefox Full support · Desktop & Mobile
Full support
Apple Safari Full support · macOS & iOS
Full support
Microsoft Edge Full support · Chromium
Full support
Opera Full support · Modern versions
Full support
Internet Explorer No PushEvent / service workers
Not supported
PushEvent() Excellent

Bottom line: Use new PushEvent in service workers; listen for real pushes with addEventListener on self.

Conclusion

new PushEvent(type, options) builds a PushEvent in a service worker. Pass "push" or "pushsubscriptionchange", optionally set data, then read event.data with text() or json(). In production, the browser delivers real push events—the constructor is mainly for tests and learning.

Continue with navigator.serviceWorker, Worker(), Event(), or the JavaScript hub.

💡 Best Practices

✅ Do

  • Run PushEvent code inside a service worker script
  • Use event.data.json() for structured server payloads
  • Call event.waitUntil() in real push handlers
  • Feature-detect "serviceWorker" in navigator on pages
  • Handle missing event.data for silent pushes

❌ Don’t

  • Expect PushEvent on window in a normal page
  • Confuse the constructor with browser-delivered push events
  • Skip secure context (HTTPS) for push features
  • Assume json() works on invalid JSON strings
  • Block the worker without waitUntil during notifications

Key Takeaways

Knowledge Unlocked

Five things to remember about PushEvent()

Service-worker push events and payload data.

5
Core concepts
📦02

.data

PushMessageData

Payload
🔒03

SW only

not on window

Context
🔄04

push / change

event types

Types
🎯05

Baseline

since Mar 2023

Status

❓ Frequently Asked Questions

new PushEvent(type, options) creates a PushEvent object. It is exposed only in a service worker context and represents a push message received from an application server.
No. MDN marks the PushEvent() constructor as Baseline Widely available (since March 2023). It is not Deprecated, Experimental, or Non-standard.
Only inside a service worker global scope, in a secure context (HTTPS or localhost). It is not available on the main window thread.
MDN says the type is case-sensitive. Browsers use push for incoming push messages and pushsubscriptionchange when a push subscription changes.
An optional data value (string, ArrayBuffer, or similar). The resulting PushEvent.data property is a PushMessageData object you can read with text(), json(), arrayBuffer(), or blob().
addEventListener listens for push events the browser delivers. The constructor lets you create a PushEvent object manually—useful for tests or learning how event.data is shaped.
Did you know?

MDN’s PushEvent page shows a full push listener that reads event.data?.json() and calls showNotification—that is how most PWAs surface push messages to users.

Read push payloads

Learn PushEvent.data—the PushMessageData on every push.

data property →

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.

5 people found this page helpful