Course contentsShow
← Web Development
Lesson 2349 of 2,98242. Progressive Web AppsFree lesson

Sending Subscription to Your Server

Serializing the PushSubscription object and sending it to your backend for storage.

Sending Subscription to Your Server

What you'll learn: How to serialize a PushSubscription object and send it to your backend so you can store it and send push notifications later.


When you create a push subscription on the client (as you did in the previous lesson), that subscription object only exists in the browser. To actually send push notifications to that user, your server needs to know about this subscription and store it in your database.

Why Serialization Matters

A PushSubscription object contains everything your server needs: an endpoint URL and encryption keys. But it's a JavaScript object with methods attached—you can't just send it over the network directly. You need to convert it to plain JSON data first.

Converting and Sending

The PushSubscription object provides a toJSON() method that extracts all the important data into a plain object. Then you send it to your backend via fetch:

// After creating the subscription...
const subscriptionData = subscription.toJSON();

fetch('/api/subscribe', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json'
  },
  body: JSON.stringify(subscriptionData)
});

The serialized data includes three key parts:

  • endpoint - The unique push service URL
  • keys.p256dh - Public key for encryption
  • keys.auth - Authentication secret

Backend Storage

Your server should receive this data, validate it, and store it associated with the current user. You'll need this exact subscription data later when you want to trigger a push notification to that specific device.

Think of it like getting someone's mailing address—once they give it to you, you write it down in your address book so you can send them letters later.

Key Takeaway: Use subscription.toJSON() to serialize your PushSubscription object, then send it to your backend via a POST request so your server can store it and send push notifications to that device in the future.