services/notification/pod-notification/README.md
A microservice for sending push notifications via web push protocol.
The notification service provides endpoints for sending web push notifications to subscribed clients. It uses the Web Push Protocol with VAPID (Voluntary Application Server Identification) for secure delivery of notifications.
The service is configured via environment variables:
| Variable | Required | Default | Description |
|---|---|---|---|
PORT | Yes | 8091 | Port number for the service |
SOURCE | Yes | - | Source identifier for the service |
AUTH_TOKEN | No | - | Bearer token for API authentication |
PUSH_PUBLIC_KEY | No | - | VAPID public key for web push |
PUSH_PRIVATE_KEY | No | - | VAPID private key for web push |
PUSH_SUBJECT | No | mailto:[email protected] | VAPID subject (email or URL) |
To generate VAPID keys, you can use the web-push library:
npx web-push generate-vapid-keys
This will output a public and private key pair that should be set in the environment variables.
npm install
rushx run-local
docker build -t notification-service .
docker run -p 8091:8091 \
-e SOURCE=notification \
-e PUSH_PUBLIC_KEY=your_public_key \
-e PUSH_PRIVATE_KEY=your_private_key \
notification-service
/web-pushSends web push notifications to subscribed clients.
Authorization: Bearer <token> (if AUTH_TOKEN is configured){
"data": {
"title": "Notification Title",
"body": "Notification message",
"icon": "/icon.png",
"badge": "/badge.png",
"tag": "notification-tag",
"url": "/target-url"
},
"subscriptions": [
{
"_id": "subscription-id",
"endpoint": "https://fcm.googleapis.com/fcm/send/...",
"keys": {
"p256dh": "client-public-key",
"auth": "client-auth-secret"
}
}
]
}
{
"result": ["subscription-id-1", "subscription-id-2"]
}
The result array contains IDs of subscriptions that failed due to:
These subscription IDs should be removed from your database.
data or subscriptions in request bodyThe data object supports the following properties:
| Property | Type | Required | Description |
|---|---|---|---|
title | string | Yes | Notification title |
body | string | No | Notification body text |
icon | string | No | URL to notification icon |
badge | string | No | URL to notification badge |
tag | string | No | Tag for grouping notifications |
url | string | No | URL to open when notification is clicked |
data | object | No | Custom data payload |
// Register service worker
const registration = await navigator.serviceWorker.register('/sw.js');
// Subscribe to push notifications
const subscription = await registration.pushManager.subscribe({
userVisibleOnly: true,
applicationServerKey: 'your-vapid-public-key'
});
// Send subscription to your server
await fetch('/api/subscribe', {
method: 'POST',
body: JSON.stringify(subscription),
headers: { 'Content-Type': 'application/json' }
});
self.addEventListener('push', event => {
const data = event.data ? event.data.json() : {};
const options = {
body: data.body,
icon: data.icon,
badge: data.badge,
tag: data.tag,
data: { url: data.url }
};
event.waitUntil(
self.registration.showNotification(data.title, options)
);
});
self.addEventListener('notificationclick', event => {
event.notification.close();
if (event.notification.data?.url) {
event.waitUntil(
clients.openWindow(event.notification.data.url)
);
}
});
The service automatically handles common web push errors:
Applications should monitor the response and remove failed subscription IDs from their database.
The service logs the following events:
src/
├── main.ts # Main application entry point
├── config.ts # Configuration management
├── server.ts # Express server setup
└── types.ts # TypeScript type definitions
"No VAPID keys configured"
PUSH_PUBLIC_KEY and PUSH_PRIVATE_KEY are set"Invalid auth token"
AUTH_TOKEN environment variablePush notifications not delivered
High subscription failure rate