Skip to content

Service

constructor

Signature

js
new Service(options)

Description

A Feathers service for sending web push notifications to subscribed users.

This service retrieves subscriptions from a specified Feathers service, validates them, and sends notifications using the web-push library. It handles successful and failed deliveries and logs relevant debug information.

Constructor Parameters

NameTypeRequiredDescription
optionsobjectyesConfiguration object for the service.
options.appobjectyesThe Feathers application instance.
options.vapidDetailsobjectyesVAPID key details for sending push notifications. Must contain subject, publicKey, and privateKey.

Throws (constructor)

TypeDescription
ErrorIf options, options.app, or options.vapidDetails are missing.

create

Signature

js
service.create(data, params)

Description

Sends a web push notification to all subscriptions retrieved from the specified service.

Steps:

  1. Validates the required parameters in data:
    • notification
    • subscriptionService
    • subscriptionProperty
  2. Retrieves subscriptions from the specified service using an optional subscriptionFilter.
  3. Iterates over each subscription, validates the subscription keys (endpoint, keys.auth, keys.p256dh).
  4. Sends the notification using web-push.sendNotification.
  5. Returns an object containing arrays of successful and failed deliveries.

Parameters

NameTypeRequiredDescription
dataobjectyesData for sending the notification.
data.notificationobjectyesThe notification payload.
data.subscriptionServicestringyesName of the Feathers service storing subscriptions.
data.subscriptionPropertystringyesProperty containing subscriptions in the service.
data.subscriptionFilterobjectnoOptional filter/query for retrieving subscriptions.
paramsobjectnoFeathersJS service call params.

Returns

TypeDescription
Promise<object>An object containing:
- succesful: array of successful notifications
- failed: array of errors
- subscriptionService and subscriptionProperty used.

Throws (create)

TypeDescription
BadRequestIf required data parameters are missing or if subscription object is invalid.

Examples

js
import { Service } from '@kalisio/feathers-webpush/server'

const pushService = new Service({
  app,
  vapidDetails: {
    subject: 'mailto:admin@example.com',
    publicKey: process.env.VAPID_PUBLIC_KEY,
    privateKey: process.env.VAPID_PRIVATE_KEY
  }
})

const result = await pushService.create({
  notification: { title: 'Hello!', body: 'Test notification' },
  subscriptionService: 'users',
  subscriptionProperty: 'subscriptions'
})

console.log(result.succesful, result.failed)