Power Up Your App with Server-Sent Push Notifications

Leader posted 1 min read

You will need a server-side implementation to send push notifications. Here’s an example using Node.js with the web-push library.

Install web-push
npm install web-push
Server Code Example

server.js

const webPush = require('web-push');

// Replace with your own VAPID keys
const vapidKeys = webPush.generateVAPIDKeys();

webPush.setVapidDetails(
  'mailto:*Emails are not allowed*',
  vapidKeys.publicKey,
  vapidKeys.privateKey
);

// Example subscription object (should be obtained from the client)
const subscription = {
  endpoint: 'USER_SUBSCRIPTION_ENDPOINT',
  keys: {
    auth: 'USER_AUTH_KEY',
    p256dh: 'USER_P256DH_KEY',
  },
};

// Send a push notification
const payload = JSON.stringify({
  title: 'Hello!',
  body: 'This is a push notification from Angular!',
});

webPush.sendNotification(subscription, payload)
  .then(response => console.log('Notification sent successfully:', response))
  .catch(error => console.error('Error sending notification:', error));

Running the Application

  1. Serve your Angular application:
   ng serve --prod
  1. Open your browser and navigate to your application.

  2. Accept the notification permissions.

  3. Trigger a push notification from your server.

Summary

  • Setting up Angular service workers for push notifications.
  • Requesting notification permissions in the Angular app.
  • Sending push notifications from a Node.js server.

Make sure to replace YOUR_PUBLIC_VAPID_KEY with your actual VAPID key from your server-side implementation.

To see the frontend implementation, check out this post.
https://coderlegion.com/3273/leveraging-service-workers-to-enhance-web-app-performance-and-reliability

If you read this far, tweet to the author to show them you care. Tweet a Thanks

Thanks for the clear walkthrough—really helpful for getting started with push notifications! Have you tried handling notification click events or background sync with service workers too? Would love to hear how you approached that part.

Thank you for the article! It’s concise, informative, and straight to the point—no unnecessary fluff. Really enjoyed reading it!

Thanks @stjam for the feedback.

Thank you, straight-forward.

More Posts

How to set up TypeScript with Node.js and Express (2025)

Sunny - Jun 6

Express and TypeScript: How to Set Up Your Project

Mubaraq Yusuf - May 26

How to login to your remote server like Digital Ocean using SSH key on window

Simeon michael - May 22

How to set up TypeScript with Node.js and Express

Sunny - Jul 13

Angular Gets Static: Exploring AnalogJS and the Power of SSG

Sunny - Sep 25
chevron_left