Contact [email protected] for all questions
PubNub Real-time Data Network
- You need only composer folder. To get it clone repo:
``` sh
$ git clone https://github.com/pubnub/php.git ./pubnub-php
```
- Copy composer/lib folder to your project and include autoloader.php file.
- Require autoloader.php:
```php
require_once('lib/autoloader.php');
```
- Add pubnub package to your composer.json file:
``` json
{
"require": {
"pubnub/pubnub": "3.7.*"
}
}
```
- Run
composer install
from command line
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
));
$info = $pubnub->publish('my_channel', 'Hey World!');
print_r($info);
$timestamp = $pubnub->time();
var_dump($timestamp); ## Prints integer timestamp.
$pubnub->subscribe('my_channel', function($message) {
var_dump($message); ## Print Message
return true; ## Keep listening (return false to stop)
});
// will subscribe to *my_channel-pnpres* channel
$pubnub->presence('my_channel', function($message) {
print_r($message);
echo "\r\n";
return false;
});
// 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 = $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 = $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
)
$result = $pubnub->whereNow('user_uuid');
print_r($result);
will output:
Array
(
[status] => 200
[message] => OK
[payload] => Array
(
[channels] => Array
(
[0] => demo_channel
)
)
[service] => Presence
)
$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
)
Checkout channel/subkey/user-level grant/audit/revoke examples here
$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");
});
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");