Get started with a free 7-day trial on our Startup plan.

start trial

PushRadar and PHP

The latest version of PushRadar's API is v2, released July 5, 2019.

Welcome to PushRadar's API documentation for PHP. This article covers calling the various methods of the PushRadar object to publish messages to a channel in realtime.

(PHP)Prerequisites

PushRadar's PHP library supports PHP 5.3+. Please ensure you have enabled the curl and json PHP extensions.

  • -To install PushRadar's PHP library using Composer, run the command composer require pushradar/pushradar-php
(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.

  • PHP
use PushRadar\PushRadar;

$radar = new PushRadar("your-secret-key");
$radar->broadcast("test-channel", array("message" => "Hello World!"));
(client)Receiving messages

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

(tip)Adding multiple data items

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

  • PHP
// ... $radar = new PushRadar(...);
$radar->addDataItems(array(
    "message" => "Hello World!",
    "item_2" => "It's me!"
))->broadcast("test-channel");

// --- or ---

$radar->broadcast("test-channel", array("message" => "Hello World!", "item_2" => "It's me!"));
(security)Private channels

PushRadar supports private channels, which allow you to ensure that certain channels are only subscribed to by certain users.

The process is as follows. First you must set up an authentication endpoint, a URL that PushRadar will call to authenticate users when they subscribe to a channel. From this endpoint, you will need to check whether the user is allowed to subscribe to the desired channel. If they are, the endpoint should return an authentication token which can be generated using the channelAuth method.

The final step is to register your authentication endpoint using the JavaScript client library.

Note:   Private channels must start with the prefix "private-"

Here is some code for the Laravel framework showing the whole process:

  • PHP
namespace App\Http\Controllers;

use PushRadar\PushRadar;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class SiteController extends Controller
{
    public function doChannelAuth(Request $request)
    {
        // Create the PushRadar object
        $radar = new PushRadar("your-secret-key");

        // Get request query parameter "channel"
        $channel = $request->query("channel");
        $user = Auth::user();

        $userCanJoinChannel = true; // TODO: determine whether $user can join $channel

        // ... if they can, return the auth token
        if ($userCanJoinChannel) {
            return ["authToken" => $radar->channelAuth($channel)];
        } else {
            App::abort(403);
        }
    }
}

Register the route in web.php:

  • PHP
use Illuminate\Support\Facades\Route;

Route::get("/channel-auth", "SiteController@doChannelAuth");

Lastly, register your authentication endpoint URL using the JavaScript client library:

  • JavaScript
var radar = new PushRadar("your-api-key");
radar.setAuthenticationEndpoint("http://localhost/channel-auth");
(security)Encrypted channels

PushRadar also supports end-to-end channel encryption, allowing you to protect sensitive data. Encrypted channels, like private channels, require authentication.

Note:   Encrypted channels must start with the prefix "private-encrypted-"

The process is almost identical to that when using private channels. The only difference is that your authentication endpoint should return a decryption key along with an authentication token as shown below.

Note:   You should use a long, hard to guess master passphrase as an encryption key.

  • PHP
// $radar = new PushRadar(...);

// Set a master key passphrase
$radar->setEncryptionMasterKey("master-key-passphrase");

// Get request query parameter "channel" and determine whether the user can join it (see private channels)
$channel = ...
$userCanJoinChannel = ...

// ... if they can, return the auth token and decryption key
if ($userCanJoinChannel) {
    return [
        "authToken" => $radar->channelAuth($channel),
        "decryptionKey" => $radar->generateDecryptionKey($channel)
    ];
} else {
    App::abort(403);
}

Note:   Remember to register your authentication endpoint URL. See private channels for more info.