This package is an implementation of the Pusher Beams Node Server SDK suitable for use in environments like Cloudflare Workers that have the Web Fetch API, but no access to node utilities.
I've ported this code over from the 1.2.4 SDK at time of writing and will do my best to update where required. In terms of compatability, I've implemented most of the tests at in the Node Server repo and have them passing - although most of the tests are for ensuring the correct params are passed through.
If you'd like to support the project: Buy me a coffee!
import { PushNotifications } from 'pusher-beams-fetch';
const pn = new PushNotifications({
instanceId: 'some value',
secretKey: 'something else'
});
const generatedToken = await pn.generateToken('foo@bar.com');
// return a JSON response with the generatedToken using your framework of choice.
// generatedToken is an object with a property token: string
return new Response(JSON.stringify(generatedToken), {
headers: {
"Content-Type": "application/json; charset=utf-8"
}
});
new PushNotifications(options)
Construct a new Pusher Beams Client connected to your Beams instance.
You only need to do this once.
instanceId
(String | Required) - The unique identifier for your Beams instance. This can be found in the dashboard under "Credentials".secretKey
(String | Required) - The secret key your server will use to access your Beams instance. This can be found in the dashboard under "Credentials".
A Pusher Beams client
import { PushNotifications } from 'pusher-beams-fetch';
const pn = new PushNotifications({
instanceId: 'some value',
secretKey: 'something else',
});
Publish a push notification to devices subscribed to given Interests, with the given payload.
Interests to send the push notification to, ranging from 1 to 100 per publish request. See Concept: Device Interests.
(Promise) - A promise that resolves to a publishResponse
. See publish API reference.
await pn.publishToInterests(['hello'], {
apns: {
aps: {
alert: {
title: 'Hello',
body: 'Hello, world!',
},
},
},
fcm: {
notification: {
title: 'Hello',
body: 'Hello, world!',
},
},
web: {
notification: {
title: 'Hello',
body: 'Hello, world!',
},
},
});
Publish a push notification to devices belonging to specific users, with the given payload.
User IDs to send the push notification to, ranging from 1 to 1000 per publish request. See Concept: Authenticated Users.
(Promise) - A promise that resolves to a publishResponse
. See publish API reference.
await pn.publishToUsers(['user-001', 'user-002'], {
apns: {
aps: {
alert: {
title: 'Hello',
body: 'Hello, world!',
},
},
},
fcm: {
notification: {
title: 'Hello',
body: 'Hello, world!',
},
},
web: {
notification: {
title: 'Hello',
body: 'Hello, world!',
},
},
});
Generate a Beams auth token to allow a user to associate their device with their user id. The token is valid for 24 hours.
User ID of the user for whom you want to generate a Beams token.
(Promise) - A promise that resolves with a Beams token for the given user.
const beamsToken = await pn.generateToken('user-001');
Delete a user and all their devices from Pusher Beams.
The user ID of the user you wish to delete.
(Promise) - A promise that resolves with no arguments. If deletion fails, the promise will reject.
await pn.deleteUser('user-001');