Node.js Server SDK

Installation

The Beams Node.js server SDK is available on npm here.

Install Using Npm

You can install this SDK by using npm:

bash

1
$ npm install @pusher/push-notifications-server --save

Install Using Yarn

Or yarn if you prefer:

bash

1
$ yarn add 

Usage

Configuring the SDK for Your Instance

Use your instance id and secret key (you can get these from the dashboard) to create a PushNotifications instance:

JavaScript

1
2
3
4
5
6
const PushNotifications = require('@pusher/push-notifications-server');

let pushNotifications = new PushNotifications({
  instanceId: 'YOUR_INSTANCE_ID_HERE',
  secretKey: 'YOUR_SECRET_KEY_HERE'
});

Publishing a Notification

Once you have created your PushNotifications instance you can publish a push notification to your registered and subscribed devices:

JavaScript

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
pushNotifications.publish(['hello'], {
  apns: {
    aps: {
      alert: 'Hello!'
    }
  },
  fcm: {
    notification: {
      title: 'Hello',
      body: 'Hello, world!'
    }
  }
}).then((publishResponse) => {
  console.log('Just published:', publishResponse.publishId);
}).catch((error) => {
  console.log('Error:', error);
});

Reference

Class: PushNotifications (Default Export)

new PushNotifications(options)

Construct a new Pusher Beams Client connected to your Beams instance.

Arguments
options:

  • instanceId(string | required): The unique identifier for your Push notifications instance. This can be found in the dashboard under "keys".
  • secretKey(string | required): The secret key your server will use to access your Beams instance. This can be found in the dashboard under "keys".

pushNotifications.publish(interests, publishBody)

Publish a new push notification to Pusher Beams with the given payload.

Arguments
interests: Array of interests to send the push notification to, ranging from 1 to 100 per publish request. See Interests.
publishBody: See publish API reference

Returns
A promise that resolves to apublishResponse: See publish API reference

Did you find this document useful?

We are always striving to create the most accurate and informative docs as possible. If there is something especially wrong (or right) here then please let us know.