PushRadar and Node.js

The latest version of PushRadar's API is v1, released August 21, 2017.

Welcome to PushRadar's API documentation for Node.js. This article takes you through publishing notifications to a channel in realtime.

(Node.js)Prerequisites

In order to use PushRadar with Node.js:

  • (a)Make sure that you have Node.js installed on your server.
  • (b)Install PushRadar's library by running the command npm install pushradar --save
(server)"Hello World!" example

The following example demonstrates broadcasting a "Hello World!" message on a channel called test-channel. Replace your-secret-key with your actual PushRadar secret, which may be obtained from the API page of your dashboard.

  • Node.js
var radar = require("pushradar")("your-secret-key");
radar.broadcast("test-channel", {"message": "Hello World!"});
(client)Receiving notifications

To find out how to receive notifications by subscribing to channels, check out the documentation for our JavaScript client library.

(tip)Adding multiple data items

For convenience, PushRadar's Node.js library supports a fluent syntax to structure broadcasts. To add multiple data items, you can do either of the following:

  • Node.js
// ... var radar = require("pushradar")("...");
radar.addDataItem("message", "Hello World!").addDataItem("item-2", "It's me!")
    .broadcast("test-channel");

// --- or ---

radar.addDataItems({
    "message": "Hello World!",
    "item-2": "It's me!"
}).broadcast("test-channel");
(feature)Action targeting

The following example demonstrates sending a notification to users who have taken a specific action, in this case, users who have purchased item #11.

  • Node.js
var radar = require("pushradar")("your-secret-key");
radar.targetAction("purchased-item-#11").addDataItem("message", "You have purchased item 11.")
    .broadcast("test-channel");

If you include more than one action by calling targetAction() again or targetActions(), PushRadar will send the notification to users who have taken any of the given actions.

With the PushRadar JavaScript client library, you can track actions that users have taken using the action method:

  • JavaScript
// ... var radar = new PushRadar(...)
radar.action("purchased-item-#11");
Reverse action targeting

To send a notification to those who have not taken a certain action, you can use the targetNotAction and targetNotActions methods. If you specify more than one action identifier, PushRadar will only send the notification to clients who have not taken any of the given actions.

  • Node.js
var radar = require("pushradar")("your-secret-key");
radar.targetNotAction("purchased-item-#12")
    .addDataItem("message", "You have not purchased item 12.")
    .broadcast("test-channel");
(feature)User targeting

User targeting allows you to broadcast notifications to specific users, identified by their user ID. For this example, we will send a new message to the user with ID usr100 in our system by calling targetUser(). Similarly, to target multiple users, you can call targetUsers().

  • Node.js
var radar = require("pushradar")("your-secret-key");
radar.targetUser("usr100").addDataItem("message", "Hey there, from Sam!")
    .addDataItem("sender_name", "Sam").broadcast("user:new-message");

Make sure to set user IDs on each website page load using the PushRadar JavaScript client library:

  • JavaScript
// Set user ID on initialization
var radar = new PushRadar("your-api-key", "usr100");

// ... or ... radar.setUserID("usr100");

radar.subscribe("user:new-message", function(notification) {
    alert("New message from " + notification.sender_name); // "New message from Sam"
});
(feature)Browser targeting

You can target notifications to users who are currently using specific web browsers by calling targetBrowser() or targetBrowsers(). In the following example, we broadcast a notification to users currently using Firefox, Chrome or Internet Explorer:

Supported browsers are:   "chrome", "firefox", "ie", "edge", "safari" and "opera"

  • Node.js
var radar = require("pushradar")("your-secret-key");
radar.targetBrowsers(["firefox", "chrome", "ie"]).addDataItem("data",
    "This notification is browser-targeted.").broadcast("test-channel");
(feature)Geo-targeting (ISO 3166-1 alpha-2 )

You can easily broadcast notifications to clients located in certain countries by calling targetCountry() or targetCountries(), passing in two-letter ISO country codes. In this example, we broadcast a notification to clients who are currently located in the UK or France:

  • Node.js
var radar = require("pushradar")("your-secret-key");
radar.targetCountries(["GB", "FR"]).addDataItem("data", "You are in the UK or France!")
    .broadcast("test-channel");

Continent targeting is also supported, with specific target... methods you can call. Below, we add to the previous example and broadcast to all users located in North America, the UK or France:

  • Node.js
var radar = require("pushradar")("your-secret-key");
radar.targetNorthAmerica().targetCountries(["GB", "FR"])
    .addDataItem("data", "You are in North America, the UK or France!")
    .broadcast("test-channel");

Each continent has its own dedicated "target" method:

   - targetEurope()
   - targetNorthAmerica()
   - targetSouthAmerica()
   - targetOceania()
   - targetAsia()
   - targetAfrica()
   - targetAntarctica()

(feature)IP address targeting

PushRadar supports IP address targeting. When using IP targeting, only clients with the given IP address(es) specified will receive your notification. Below is an example targeting a client with IP address 216.3.128.12. The targetIP and targetIPs methods accept both IPv4 and IPv6 addresses.

  • Node.js
var radar = require("pushradar")("your-secret-key");
radar.targetIP("216.3.128.12").addDataItem("data", "This notification is IP-targeted.")
    .broadcast("test-channel");