This project demonstrates how to build a browser push notification system using Node.js, Express, and the Web Push API. It leverages Service Workers to receive push notifications in the browser and uses the web-push library on the server to send notifications.
-
Service Worker Registration:
Registers a service worker to enable background processing of push events. -
Push Subscription:
Uses the Push API to subscribe the browser for push notifications. -
VAPID Authentication:
Utilizes VAPID keys to securely send notifications. -
Express Server:
Handles subscription storage and notification dispatching.
- Node.js (v12 or later)
- A modern browser (Chrome, Firefox, etc.) that supports Service Workers and the Push API
project-root/ ├── public/ │ ├── index.html # Client-side HTML and JavaScript │ └── sw.js # Service Worker file ├── generateVapidKeys.js # Script to generate VAPID keys ├── server.js # Express server code ├── package.json └── README.md git clone <repository-url> cd <repository-folder>npm installRun the following command to generate your VAPID keys:
node generateVapidKeys.jsThis will output an object containing publicKey and privateKey. Update your code in both server.js and index.html:
- In
server.js: Replace'YOUR_PUBLIC_VAPID_KEY_HERE'and'YOUR_PRIVATE_VAPID_KEY_HERE'with the generated keys. - In
index.html: Replace'YOUR_PUBLIC_VAPID_KEY_HERE'with your public key.
Run the Express server:
node server.jsYour server will start on http://localhost:3000.
-
Open your browser and navigate to
http://localhost:3000. -
Click the "Subscribe for Notifications" button. This will register the service worker and subscribe your browser for push notifications.
-
To send a push notification, use a tool like
curlor Postman to send a POST request to the/notifyendpoint:curl -X POST http://localhost:3000/notify
You should receive a push notification in your browser.
-
Client Side:
-
Service Worker Registration:
The browser registerssw.jsas a service worker. -
Subscription:
The browser subscribes for push notifications using the Push API. The subscription object (including endpoint and keys) is sent to the server via the/subscribeendpoint. -
Handling Push Events:
The service worker listens forpushevents and displays notifications using the Notifications API.
-
-
Server Side:
-
Subscription Storage:
The server stores incoming subscription objects (for demo purposes, these are kept in memory; consider using a database in production). -
Sending Notifications:
The/notifyendpoint uses theweb-pushlibrary to send notifications to all stored subscriptions using the VAPID keys for authentication.
-
-
HTTPS:
Service Workers and push notifications require HTTPS in production environments (except onlocalhostduring development). -
Subscription Storage:
Replace the in-memory storage with a database for persistent storage of subscription objects. -
Error Handling:
Enhance error handling and logging for a production-ready system.