Skip to content

leopardhs/php

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Contact [email protected] for all questions

PubNub Real-time Data Network

Clients for PHP and Composer

How to include

PHP >= 5.3 without composer

  1. You need only composer folder. To get it clone repo:
``` sh
$ git clone https://github.com/pubnub/php.git ./pubnub-php
```
  1. Copy composer/lib folder to your project and include autoloader.php file.
  2. Require autoloader.php:
```php
require_once('lib/autoloader.php');
```

PHP >= 5.3 with composer

  1. Add pubnub package to your composer.json file:
``` json
{
    "require": {
        "pubnub/pubnub": "3.7.*"
    }
}
```
  1. Run composer install from command line

PHP API

You can instantiate PubNub client using postional list of arguments:

use Pubnub\Pubnub;

$pubnub = new Pubnub(
    "demo",  ## PUBLISH_KEY
    "demo",  ## SUBSCRIBE_KEY
    "",      ## SECRET_KEY
    false    ## SSL_ON?
);

or you can use named array:

use Pubnub\Pubnub;

$pubnub = new Pubnub(array(
    'subscribe_key' => 'demo',
    'publish_key' => 'demo',
    'uuid' => 'my_uu_id',
    'ssl' => true
));

Send Message (PUBLISH)

$info = $pubnub->publish('my_channel', 'Hey World!');

print_r($info);

Request Server Time (TIME)

$timestamp = $pubnub->time();
var_dump($timestamp);            ## Prints integer timestamp.

Receive Message (SUBSCRIBE)

$pubnub->subscribe('my_channel', function($message) {
    var_dump($message);  ## Print Message
    return true;         ## Keep listening (return false to stop)
});

Realtime Join/Leave Events (Presence)

// will subscribe to *my_channel-pnpres* channel
$pubnub->presence('my_channel', function($message) {
    print_r($message);
    echo "\r\n";
    return false;
});

On-demand Occupancy Status (here_now)

// all users of specific channel with uuids
$here_now = $pubnub->hereNow('my_channel');

// all users of specific channel without uuids
$here_now = $pubnub->hereNow('my_channel', true);

// all users of specific channel with state info
$here_now = $pubnub->hereNow('my_channel', false, true);

// all users of all channels for given subscribe key
$here_now = $pubnub->hereNow();

History (detailedHistory())

$history = $pubnub->history('demo', 3, false, false, 13466530169226760);

print_r($history);

will output:

Array
(
    [messages] => Array
        (
            [0] => message #1
            [1] => message #2
            [2] => message #3
        )

    [date_from] => 14037149868340218
    [date_to] => 14037149868888352
)

History (detailedHistory()) with time tokens

$history = $pubnub->history('demo', 3, true);

print_r($history);

will output:

Array
(
    [messages] => Array
        (
            [0] => Array
                (
                    [message] => message #1
                    [timetoken] => 14037149868340218
                )

            [1] => Array
                (
                    [message] => message #2
                    [timetoken] => 14037149868613433
                )

            [2] => Array
                (
                    [message] => message #3
                    [timetoken] => 14037149868888352
                )
        )
    [date_from] => 14037149868340218
    [date_to] => 14037149868888352
)

Current channels for given subscriber (whereNow)

$result = $pubnub->whereNow('user_uuid');

print_r($result);

will output:

Array
(
    [status] => 200
    [message] => OK
    [payload] => Array
        (
            [channels] => Array
                (
                    [0] => demo_channel
                )

        )

    [service] => Presence
)

User state information (setState/getState)

$pubnub->setState('demo', array('name' => 'Mike', 'status' => 'busy', 'age' => 30));
$result = $pubnub->getState('demo', $pubnub->getUUID());

print_r($result);

will output:

Array
(
    [status] => 200
    [uuid] => DE2BE11A-9ABE-4ACE-B742-8B0508112619
    [service] => Presence
    [message] => OK
    [payload] => Array
        (
            [status] => busy
            [age] => 30
            [name] => Mike
        )

    [channel] => demo
)

PAM Grant/Audit/Revoke

Checkout channel/subkey/user-level grant/audit/revoke examples here

PHP HTTP Pipelining usage

$pubnub->pipeline(function ($p) {
      $p->publish('my_channel', "Pipelined message #1");
      $p->publish('my_channel', "Pipelined message #2");
      $p->publish('my_channel', "Pipelined message #3");
});

Channel Groups management

To use namespaces just add namespace name with ":" prior to group name string. For example in news:music, news is namespace and music is group and news_music is top-level channel group.

// Add channels to group:
$pubnub->channelGroupAddChannel("music_news", ["jazz_news", "rock_news", "punk_news"]);
$pubnub->channelGroupAddChannel("news:music", ["jazz_news", "rock_news", "punk_news"]);

// Get group channels list
$pubnub->channelGroupListChannels("music_news");
$pubnub->channelGroupListChannels("news:music");

// Remove channel from group
$pubnub->channelGroupRemoveChannel("music_news", ["rock_news"]);
$pubnub->channelGroupRemoveChannel("news:music", ["rock_news"]);

// Subscribe to channel
$pubnub->channelGroupSubscribe("music_news", function ($result) {
    print_r($result);
});
$pubnub->channelGroupSubscribe("news:music", function ($result) {
    print_r($result);
});

// Gets a list of uuids subscribed to the channel groups.
$pubnub->channelGroupHereNow("news:music");

// Remove channel group
$pubnub->channelGroupRemoveGroup("music_news");
$pubnub->channelGroupRemoveGroup("news:music");

// Remove namespace
$pubnub->channelGroupRemoveNamespace("news");

Contact [email protected] for all questions

About

PubNub clients for PHP

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 49.8%
  • JavaScript 44.6%
  • CSS 5.6%