Node.js Server SDK

The SDK includes a typings file for TypeScript compatibility.

Installation

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

Install

You can install this SDK by using NPM or Yarn:

  • NPM
  • Yarn
$ npm install @pusher/push-notifications-server --save

Reference

constructor PushNotifications

new PushNotifications(options)

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

You only need to do this once.

Arguments
options (object)

  • 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".

Returns
A Pusher Beams client

Example

JavaScript

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

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

.publishToInterests

Publish a push notification to devices subscribed to given Interests, with the given payload.

Arguments
interests (Array<string>) - Interests to send the push notification to, ranging from 1 to 100 per publish request. See Concept: Device Interests.
publishBody (object) - See publish API reference.

Returns
(Promise) - A promise that resolves to apublishResponse. See publish API reference.

Example

JavaScript

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

.publishToUsers

Publish a push notification to devices belonging to specific users, with the given payload.

Arguments
userIDs (Array<string>) User IDs to send the push notification to, ranging from 1 to 1000 per publish request. See Concept: Authenticated Users.
publishBody (object) - See publish API reference.

Returns
(Promise) - A promise that resolves to apublishResponse. See publish API reference.

Example

JavaScript

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

.generateToken

Generate a Beams auth token to allow a user to associate their device with their user id. The token is valid for 24 hours.

Arguments
userID (string) - User ID of the user for whom you want to generate a Beams token.

Returns
(string) - A Beams token for the given user.

Example

JavaScript

const beamsToken = beamsClient.generateToken('user-001')

.deleteUser

Delete a user and all their devices from Pusher Beams.

Arguments
userID (string) - The user ID of the user you wish to delete.

Returns
(Promise) - A promise that resolves with no arguments. If deletion fails, the promise will reject.

Example

JavaScript

1
2
3
4
5
6
7
beamsClient.deleteUser(['user-001'])
.then(() => {
  console.log('Successfully deleted the user!')
})
.catch((error) => {
  console.error('Error:', error)
});