Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 120 additions & 0 deletions examples/AccessManagerV2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php

require_once(__DIR__ . '/../vendor/autoload.php');

use PubNub\PubNub;
use PubNub\PNConfiguration;

// snippet.setup
$config = new PNConfiguration();
$config->setSecretKey(getenv('SECRET_KEY') ?: 'demo');
$config->setPublishKey(getenv('PUBLISH_KEY') ?: 'demo');
$config->setSubscribeKey(getenv('SUBSCRIBE_KEY') ?: 'demo');
$config->setUserId('example');

$pubnub = new PubNub($config);
// snippet.end

// snippet.grant_channel_authkey
$channels = ["ch1", "ch2"];
$auth_key = "blah";
$read = true;
$write = false;
$ttl_mins = 15;
try {
$result = $pubnub->grant()
->channels($channels)
->authKeys($auth_key)
->read($read)
->write($write)
->ttl($ttl_mins)
->sync();
print("Grant successful\n");
} catch (\PubNub\Exceptions\PubNubServerException $exception) {
print_r("Message: " . $exception->getMessage() . "\n");
print_r("Status: " . $exception->getStatusCode() . "\n");
echo "Original message: ";
print_r($exception->getBody());
} catch (\PubNub\Exceptions\PubNubException $exception) {
print_r("Message: " . $exception->getMessage());
}
// snippet.end

// snippet.grant_all_channels
$pubnub->grant()
->read(true)
->write(true)
->sync();
// snippet.end

// snippet.grant_specific_channel
$pubnub->grant()
->channels("my_channel")
->read(true)
->write(true)
->sync();
// snippet.end

// snippet.grant_channel_with_authkey
$pubnub->grant()
->channels("my_channel")
->read(true)
->write(false)
->authKeys("my_ro_authkey")
->ttl(5)
->sync();
// snippet.end

// snippet.grant_presence_channel
$pubnub->grant()
->channels("my_channel-pnpres")
->read(true)
->write(true)
->sync();
// snippet.end

// snippet.grant_channel_group
$result = $pubnub->grant()
->channelGroups(["cg1", "cg2", "cg3"])
->authKeys(["key1", "key2", "auth3"])
->read(true)
->write(true)
->manage(true)
->ttl(12237)
->sync();
// snippet.end

// snippet.grant_application_level
try {
$result = $pubnub->grant()
->read(true)
->write(true)
->sync();

print_r($result);
} catch (\PubNub\Exceptions\PubNubServerException $exception) {
print_r($exception->getMessage() . "\n");
print_r($exception->getStatusCode() . "\n");
print_r($exception->getBody());
} catch (\PubNub\Exceptions\PubNubException $exception) {
print_r($exception->getMessage());
}
// snippet.end

// snippet.grant_channel_level
$result = $pubnub->grant()
->channels("my_channel")
->read(true)
->write(true)
->sync();
// snippet.end

// snippet.grant_user_level
$result = $pubnub->grant()
->channels("my_channel")
->authKeys("my_authkey")
->read(true)
->write(true)
->ttl(5)
->sync();
// snippet.end
13 changes: 13 additions & 0 deletions examples/AppContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,19 @@
assert($updateChannelMetadataResult->getName() === $sampleChannels[0]['name'] . ' - Updated');
// snippet.end

// snippet.write_updated_metadata_back
// Writing the updated object back to the server
$pubnub->setChannelMetadata()
->channel($sampleChannels[0]['id'])
->meta([
"name" => $updateChannelMetadataResult->getName(),
"description" => $updateChannelMetadataResult->getDescription(),
"custom" => $updatedChannelCustom,
])
->sync();
print("Object has been updated.\n");
// snippet.end

// snippet.remove_channel_metadata
$removeChannelMetadataResult = $pubnub->removeChannelMetadata()
->channel($sampleChannels[1]['id'])
Expand Down
46 changes: 46 additions & 0 deletions examples/ChannelGroups.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

require_once(__DIR__ . '/../vendor/autoload.php');

use PubNub\PNConfiguration;
use PubNub\PubNub;
use PubNub\Exceptions\PubNubServerException;

// snippet.setup
// Create configuration
$pnConfig = new PNConfiguration();
$pnConfig->setSubscribeKey(getenv("SUBSCRIBE_KEY") ?? "demo");
$pnConfig->setPublishKey(getenv("PUBLISH_KEY") ?? "demo");
$pnConfig->setUserId("php-channel-group-demo");

// Initialize PubNub instance
$pubnub = new PubNub($pnConfig);
// snippet.end

// snippet.add_channels
$result = $pubnub->addChannelToChannelGroup()
->channels(["news", "sports"])
->channelGroup("my-group")
->sync();

echo "Channels added to group successfully!" . PHP_EOL;
// snippet.end

// snippet.list_channels
$result = $pubnub->listChannelsInChannelGroup()
->channelGroup("cg1")
->sync();
// snippet.end

// snippet.remove_channels
$pubnub->removeChannelFromChannelGroup()
->channels("son")
->channelGroup("family")
->sync();
// snippet.end

// snippet.delete_channel_group
$pubnub->removeChannelGroup()
->channelGroup("family")
->sync();
// snippet.end
101 changes: 101 additions & 0 deletions examples/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@
use PubNub\PNConfiguration;
use PubNub\PubNub;
use PubNub\CryptoModule;
use PubNub\Crypto\AesCbcCryptor;
use PubNub\Crypto\LegacyCryptor;
use PubNub\Enums\PNStatusCategory;
use PubNub\Callbacks\SubscribeCallback;
use PubNub\PubNubCrypto;
use PubNub\PubNubCryptoLegacy;

// snippet.setup
// Create a new configuration instance
Expand Down Expand Up @@ -202,6 +206,103 @@ function presence($pubnub, $presence)
$pubnub = new PubNub($pnconf);
// snippet.end

// snippet.init_non_secure
$pnConfiguration = new PNConfiguration();

$pnConfiguration->setSubscribeKey("my_sub_key");
$pnConfiguration->setPublishKey("my_pub_key");
$pnConfiguration->setSecure(false);
$pnConfiguration->setUserId("myUniqueUserId");
$pubnub = new PubNub($pnConfiguration);
// snippet.end

// snippet.init_read_only
$pnConfiguration = new PNConfiguration();

$pnConfiguration->setUserId("myUniqueUserId");
$pnConfiguration->setSubscribeKey("my_sub_key");

$pubnub = new PubNub($pnConfiguration);
// snippet.end

// snippet.use_custom_uuid
$pnconf = new PNConfiguration();

$pnconf->setSubscribeKey("mySubscribeKey");
$pnconf->setPublishKey("myPublishKey");
$pnconf->setUserId("myUniqueUserId");

$pubnub = new PubNub($pnconf);
// snippet.end

// snippet.remove_listeners
$subscribeCallback = new MySubscribeCallback();

$pubnub->addListener($subscribeCallback);

// some time later
$pubnub->removeListener($subscribeCallback);
// snippet.end

// snippet.set_user_id
$pnconf = new PNConfiguration();
$pnconf->setUserId("myUniqueUserId");
// snippet.end

// snippet.get_user_id
$pubnub->getConfiguration()
->getUserId();
// snippet.end

// snippet.set_auth_key
$pubnub->getConfiguration()
->setAuthKey("my_newauthkey");
// snippet.end

// snippet.get_auth_key
$pubnub->getConfiguration()
->getAuthKey();
// snippet.end

// snippet.get_filter_expression
$pubnub->getConfiguration()->getFilterExpression();
// snippet.end

// snippet.disable_immutable_check
$config = new PNConfiguration();
$config->setPublishKey('demo');
$config->setSubscribeKey('demo');
$config->setUserId('demo');
// not recommended, disables config immutability
$config->disableImmutableCheck();

$pn = new PubNub($config);
// snippet.end

// snippet.crypto_module_setup
// encrypts using 256-bit AES-CBC cipher (recommended)
// decrypts data encrypted with the legacy and the 256-bit AES-CBC ciphers
$pnConfiguration = new PNConfiguration();
$pnConfiguration->setSubscribeKey('demo');
$pnConfiguration->setPublishKey('demo');
$pnConfiguration->setUserId('crypto-demo');
$cryptoModule = CryptoModule::aesCbcCryptor("enigma", true);
$pnConfiguration->setCryptoModule($cryptoModule);

$pubnub = new PubNub($pnConfiguration);

// encrypts with 128-bit cipher key entropy(legacy)
// decrypts data encrypted with the legacy and the 256-bit AES-CBC ciphers
$pnConfiguration2 = new PNConfiguration();
$pnConfiguration2->setSubscribeKey('demo');
$pnConfiguration2->setPublishKey('demo');
$pnConfiguration2->setUserId('crypto-demo-legacy');
$legacyCryptoModule = CryptoModule::legacyCryptor("enigma", true);
$pnConfiguration2->setCryptoModule($legacyCryptoModule);

$pubnub2 = new PubNub($pnConfiguration2);
// snippet.end

// Verify configuration
assert($pnconf->getSubscribeKey() === (getenv('SUBSCRIBE_KEY') ?: 'demo'));
assert($pnconf->getUserId() === "filter-demo-user");
Expand Down
42 changes: 42 additions & 0 deletions examples/MessageActions.php
Original file line number Diff line number Diff line change
Expand Up @@ -491,3 +491,45 @@ function tagForAnalytics($pubnub, $channel, $messageTimetoken, $tag, $value)
}

echo "\n=== MESSAGE ACTIONS DEMO COMPLETE ===\n";

// snippet.fetch_messages_with_paging
function getMessageActionsWithPaging($pubnub, $channel, $start = null)
{
$allActions = [];
$currentStart = $start;

do {
$builder = $pubnub->getMessageActions()
->channel($channel);

if ($currentStart !== null) {
$builder->setStart($currentStart);
}

$result = $builder->sync();
$actions = $result->actions;

if (!empty($actions)) {
$allActions = array_merge($allActions, $actions);
// Get the timetoken of the last action for pagination
$lastAction = end($actions);
$currentStart = $lastAction->actionTimetoken;
} else {
break;
}
} while (!empty($actions) && count($allActions) < 20); // Limit to 20 for demo

return $allActions;
}

// Usage example
$actions = getMessageActionsWithPaging($pubnub, 'my_channel');
foreach ($actions as $action) {
echo "Type: " . $action->getType() . "\n";
echo "Value: " . $action->getValue() . "\n";
echo "UUID: " . $action->getUuid() . "\n";
echo "Message Timetoken: " . $action->getMessageTimetoken() . "\n";
echo "Action Timetoken: " . $action->getActionTimetoken() . "\n";
echo "---\n";
}
// snippet.end
5 changes: 3 additions & 2 deletions examples/MessagePersistance.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,12 +237,13 @@ public function demoHistoryReverse()
echo "Retrieving oldest 3 messages first...\n";

try {
// snippet.history_reverse
$result = $this->pubnub->history()
->channel($this->demoChannel)
->channel("my_channel")
->count(3)
->reverse(true)
->includeTimetoken(true)
->sync();
// snippet.end

echo " 📊 Reverse History Results:\n";
$messages = $result->getMessages();
Expand Down
13 changes: 13 additions & 0 deletions examples/MobilePush.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,19 @@ private function demonstrateRemoveAllChannelsFromDevice($device)
->deviceId($device['deviceId'])
->sync();
// snippet.end

// snippet.remove_all_channels_response_check
$response = $this->pubnub->removeAllPushChannelsForDevice()
->pushType(PNPushType::APNS2)
->deviceId("yourDeviceId")
->sync();

if ($response->isSuccessful()) {
echo "Successfully removed all push channels from device.";
} else {
echo "Failed to remove push channels.";
}
// snippet.end
}

echo " ✅ Successfully removed all push channels from device\n\n";
Expand Down
Loading
Loading