Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
ed76966
First batch of new tests
jakub-grzesiowski Oct 7, 2025
fe2f483
Fix linter errors
jakub-grzesiowski Oct 7, 2025
95db558
Fix linter errors
jakub-grzesiowski Oct 8, 2025
49927c4
More linter
jakub-grzesiowski Oct 8, 2025
92d25a6
Linter + remove faulty test file
jakub-grzesiowski Oct 8, 2025
b607c46
Mass PHP CBF apply
jakub-grzesiowski Oct 8, 2025
531cbd1
Even more linter fixes
jakub-grzesiowski Oct 8, 2025
eb3465d
add missing semicolons
jakub-grzesiowski Oct 8, 2025
3892687
last missing semicolon
jakub-grzesiowski Oct 8, 2025
68696be
there better not be more missing semicolons
jakub-grzesiowski Oct 8, 2025
fcbd63b
Test phpstan setting change
jakub-grzesiowski Oct 8, 2025
af97273
More phpcbf
jakub-grzesiowski Oct 8, 2025
061a49f
Fix wrong timestamp method call in tests
jakub-grzesiowski Oct 8, 2025
c7f00f0
Remove invalid test
jakub-grzesiowski Oct 8, 2025
ad3431a
Add missing snippets for docs
jakub-grzesiowski Oct 8, 2025
62d2967
Examples linter
jakub-grzesiowski Oct 9, 2025
06a8fe1
Initial review fixes
jakub-grzesiowski Oct 15, 2025
9084833
Linter fixes
jakub-grzesiowski Oct 15, 2025
6614f38
Test return type fix
jakub-grzesiowski Oct 15, 2025
4b9dfea
Potential PHPStan fixes
jakub-grzesiowski Oct 15, 2025
2379302
Revert "Potential PHPStan fixes"
jakub-grzesiowski Oct 15, 2025
1dfbeb8
Test phpstan neon file change
jakub-grzesiowski Oct 15, 2025
1733bbf
Remove ignore line
jakub-grzesiowski Oct 15, 2025
74b1afc
Return types tweak
jakub-grzesiowski Oct 15, 2025
714bbf0
Add configuration snippets verification
jakub-grzesiowski Oct 15, 2025
d734b47
Remove whitespace
jakub-grzesiowski Oct 15, 2025
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
215 changes: 215 additions & 0 deletions examples/Configuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
<?php

// @phpstan-ignore-file
// phpcs:ignoreFile
require_once __DIR__ . '/../vendor/autoload.php';

use PubNub\PNConfiguration;
use PubNub\PubNub;
use PubNub\CryptoModule;
use PubNub\Enums\PNStatusCategory;
use PubNub\Callbacks\SubscribeCallback;

// snippet.setup
// Create a new configuration instance
$pnConfiguration = new PNConfiguration();

// Set subscribe key (required)
$pnConfiguration->setSubscribeKey(getenv('SUBSCRIBE_KEY') ?? 'demo');

// Set publish key (only required if publishing)
$pnConfiguration->setPublishKey(getenv('PUBLISH_KEY') ?? 'demo');

// Set UUID (required to connect)
$pnConfiguration->setUserId('php-config-demo-user');
// snippet.end

// Verify configuration was set correctly
assert($pnConfiguration->getSubscribeKey() === (getenv('SUBSCRIBE_KEY') ?? 'demo'));
assert($pnConfiguration->getPublishKey() === (getenv('PUBLISH_KEY') ?? 'demo'));
assert($pnConfiguration->getUserId() === 'php-config-demo-user');

// snippet.basic_configuration
// Create a new configuration instance
$pnConfiguration = new PNConfiguration();

// Set subscribe key (required)
$pnConfiguration->setSubscribeKey(getenv('SUBSCRIBE_KEY') ?? 'demo');

// Set publish key (only required if publishing)
$pnConfiguration->setPublishKey(getenv('PUBLISH_KEY') ?? 'demo');

// Set UUID (required to connect)
$pnConfiguration->setUserId("php-sdk-example-user");

// Set up cryptography for message encryption (optional)
// Uncomment the line below to enable encryption
// $pnConfiguration->setCryptoModule(CryptoModule::aesCbcCryptor("your-cipher-key", true));

// Set authentication key (optional, required only when using Access Manager)
// $pnConfiguration->setAuthKey("my_auth_key");

// Configure connection timeout in seconds
$pnConfiguration->setConnectTimeout(10);

// Configure subscribe request timeout in seconds
$pnConfiguration->setSubscribeTimeout(310);

// Configure non-subscribe request timeout in seconds
$pnConfiguration->setNonSubscribeRequestTimeout(10);

// Set filter expression (optional)
// $pnConfiguration->setFilterExpression("channel == 'my-channel'");

// Create PubNub instance with the configured settings
$pubnub = new PubNub($pnConfiguration);

// Display configuration information
echo "PubNub Configuration:\n";
echo "Subscribe Key: " . $pnConfiguration->getSubscribeKey() . "\n";
echo "Publish Key: " . $pnConfiguration->getPublishKey() . "\n";
echo "User ID: " . $pnConfiguration->getUserId() . "\n";
echo "Encryption: " . ($pnConfiguration->getCryptoSafe() ? "enabled" : "disabled") . "\n";

// Now you can use this PubNub instance to publish and subscribe

// Example: Create a simple message
$message = ["text" => "Hello from PHP SDK!"];

// Example: Publish the message (uncomment to execute)
/*
$pubnub->publish()
->channel("demo-channel")
->message($message)
->sync();

echo "Message published to 'demo-channel'\n";
*/

// Keep this code running only if you plan to subscribe to messages
// Otherwise, the script will exit after publishing
// snippet.end

// Verify configuration values
assert($pnConfiguration->getSubscribeKey() === getenv('SUBSCRIBE_KEY') ?? 'demo');
assert($pnConfiguration->getPublishKey() === getenv('PUBLISH_KEY') ?? 'demo');
assert($pnConfiguration->getUserId() === "php-sdk-example-user");
assert($pnConfiguration->getConnectTimeout() === 10);
assert($pnConfiguration->getSubscribeTimeout() === 310);
assert($pnConfiguration->getNonSubscribeRequestTimeout() === 10);

// Verify PubNub instance was created
assert($pubnub instanceof PubNub);

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

$pnconf->setSubscribeKey(getenv('SUBSCRIBE_KEY') ?? 'demo');
$pnconf->setPublishKey(getenv('PUBLISH_KEY') ?? 'demo');
$pnconf->setSecure(false);
$pnconf->setUserId("myUniqueUserId");
$pubnub = new PubNub($pnconf);

// snippet.end

// Verify configuration
assert($pnconf->getSubscribeKey() === getenv('SUBSCRIBE_KEY') ?? 'demo');
assert($pnconf->getPublishKey() === getenv('PUBLISH_KEY') ?? 'demo');
assert($pnconf->getUserId() === "myUniqueUserId");
assert($pubnub instanceof PubNub);

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

$pnConfiguration->setSubscribeKey(getenv('SUBSCRIBE_KEY') ?? 'demo');
$pnConfiguration->setPublishKey(getenv('PUBLISH_KEY') ?? 'demo');
//NOTE: only server side should have secret key
$pnConfiguration->setSecretKey(getenv('SECRET_KEY') ?? 'demo');
$pnConfiguration->setUserId("myUniqueUserId");
$pubnub = new PubNub($pnConfiguration);
// snippet.end

// Verify configuration
assert($pnConfiguration->getSubscribeKey() === getenv('SUBSCRIBE_KEY') ?? 'demo');
assert($pnConfiguration->getPublishKey() === getenv('PUBLISH_KEY') ?? 'demo');
assert($pnConfiguration->getSecretKey() === getenv('SECRET_KEY') ?? 'demo');
assert($pnConfiguration->getUserId() === "myUniqueUserId");
assert($pubnub instanceof PubNub);

// snippet.event_listeners
class MySubscribeCallback extends SubscribeCallback
{
function status($pubnub, $status)
{
if ($status->getCategory() === PNStatusCategory::PNUnexpectedDisconnectCategory) {
// This event happens when connectivity is lost
} elseif ($status->getCategory() === PNStatusCategory::PNConnectedCategory) {
// Connect event. You can do stuff like publish, and know you'll get it
} elseif ($status->getCategory() === PNStatusCategory::PNDecryptionErrorCategory) {
// Handle message decryption error.
}
}

function message($pubnub, $message)
{
// Handle new message stored in message.message
}
function presence($pubnub, $presence)
{
// handle incoming presence data
}
}

$pnconf = new PNConfiguration();

$pnconf->setSubscribeKey(getenv('SUBSCRIBE_KEY') ?? 'demo');
$pnconf->setPublishKey(getenv('PUBLISH_KEY') ?? 'demo');
$pnconf->setUserId("event-listener-demo-user");

$pubnub = new PubNub($pnconf);

$subscribeCallback = new MySubscribeCallback();

$pubnub->addListener($subscribeCallback);

// Subscribe to a channel, this is not async.
// Note: This would block
// $pubnub->subscribe()
// ->channels("hello_world")
// ->execute();

// Use the publish command separately from the Subscribe code shown above.
// Subscribe is not async and will block the execution until complete.
// Note: Commented out for testing to avoid network calls
// $result = $pubnub->publish()
// ->channel("hello_world")
// ->message("Hello PubNub")
// ->sync();
//
// // Verify publish result
// assert($result->getTimetoken() > 0);
//
// print_r($result);
// snippet.end

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

$pnconf->setSubscribeKey(getenv('SUBSCRIBE_KEY') ?? 'demo');
$pnconf->setUserId("filter-demo-user");
$pnconf->setFilterExpression("userid == 'my_userid'");

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

// Verify configuration
assert($pnconf->getSubscribeKey() === "my_sub_key");
assert($pnconf->getPublishKey() === "my_pub_key");
assert($pnconf->getUserId() === "event-listener-demo-user");
assert($pubnub instanceof PubNub);
// Verify callback instance
assert($subscribeCallback instanceof SubscribeCallback);
// Verify configuration
assert($pnconf->getSubscribeKey() === "my_sub_key");
assert($pnconf->getFilterExpression() === "userid == 'my_userid'");
assert($pubnub instanceof PubNub);
25 changes: 21 additions & 4 deletions examples/Publishing.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,12 @@
// snippet.publish_with_post
$result = $pubnub->publish()
->channel("my_channel")
->message([
->message(
[
"text" => "Message using POST",
"description" => str_repeat("Post allows to publish longer messages", 750)
])
]
)
->usePost(true)
->sync();
assert($result->getTimetoken() > 0);
Expand All @@ -86,10 +88,12 @@
try {
$result = $pubnub->publish()
->channel("my_channel")
->message([
->message(
[
"text" => "Message using POST",
"description" => str_repeat("Post allows to publish longer messages", 1410)
])
]
)
->usePost(true)
->sync();
assert($result->getTimetoken() > 0);
Expand Down Expand Up @@ -119,3 +123,16 @@
assert($result->getTimetoken() > 0);
echo "Signal timetoken: {$result->getTimetoken()}\n";
// snippet.end

// snippet.publish_array
try {
$result = $pubnub->publish()
->channel("my_channel")
->message(["hello", "there"])
->meta(["name" => "Alex", "online" => true])
->sync();
print_r($result->getTimetoken());
} catch (PubNubException $error) {
echo "Error: " . $error->getMessage() . "\n";
}
// snippet.end
109 changes: 109 additions & 0 deletions examples/Subscribing.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
$pubnub = new PubNub($pnconfig);
// snippet.end

// Disable for "one class per file" rule
// phpcs:disable
// snippet.callback
// Create a custom callback class to handle messages and status updates
class MySubscribeCallback extends SubscribeCallback
Expand Down Expand Up @@ -62,6 +64,7 @@ public function presence($pubnub, $presence)
}
}
// snippet.end
// phpcs:enable

// snippet.subscribe
// Add the callback to PubNub
Expand All @@ -80,6 +83,8 @@ public function presence($pubnub, $presence)
}
// snippet.end

// Disable for the "declare symbols vs side effects" rule
// phpcs:disable
// snippet.history
// Get message history
function getHistory($pubnub, $channels)
Expand All @@ -100,6 +105,110 @@ function getHistory($pubnub, $channels)
}
}
// snippet.end
// phpcs:enable

// snippet.basic_subscribe_with_logging
use Monolog\Handler\ErrorLogHandler;

$pnconf = new PNConfiguration();

$pnconf->setPublishKey("demo");
$pnconf->setSubscribeKey("demo");
$pnconf->setUserId("php-subscriber-with-logging");

$pubnub = new PubNub($pnconf);

$pubnub->getLogger()->pushHandler(new ErrorLogHandler());

$pubnub->subscribe()->channels("my_channel")->execute();
// snippet.end

// Disable for the "one class per file" rule
// phpcs:disable
// snippet.subscribe_with_state
class MySubscribeCallbackWithState extends SubscribeCallback
{
public function status($pubnub, $status)
{
if ($status->getCategory() === PNStatusCategory::PNConnectedCategory) {
$result = $pubnub
->setState()
->channels("awesomeChannel")
->channelGroups("awesomeChannelGroup")
->state([
"fieldA" => "awesome",
"fieldB" => 10,
])
->sync();
print_r($result);
}
}

public function message($pubnub, $message)
{
}

public function presence($pubnub, $presence)
{
}
}

$subscribeCallback = new MySubscribeCallbackWithState();

$pubnub->addListener($subscribeCallback);

$pubnub->subscribe()
->channels("my_channel")
->execute();
// snippet.end
// phpcs:enable

// Disable for the "one class per file" rule
// phpcs:disable
// snippet.unsubscribe_from_channel
use PubNub\Exceptions\PubNubUnsubscribeException;

class MyUnsubscribeCallback extends SubscribeCallback
{
public function status($pubnub, $status)
{
if ($this->checkUnsubscribeCondition()) {
throw (new PubNubUnsubscribeException())->setChannels("awesomeChannel");
}
}

public function message($pubnub, $message)
{
}

public function presence($pubnub, $presence)
{
}

public function checkUnsubscribeCondition()
{
// return true or false
return false;
}
}

$pnconfig = new PNConfiguration();

$pnconfig->setPublishKey("demo");
$pnconfig->setSubscribeKey("demo");
$pnconfig->setUserId("php-unsubscribe-demo");

$pubnub = new PubNub($pnconfig);

$subscribeCallback = new MyUnsubscribeCallback();

$pubnub->addListener($subscribeCallback);

$pubnub->subscribe()
->channels("awesomeChannel")
->execute();
// snippet.end
// phpcs:enable

echo "Starting PubNub Subscriber...\n";
echo "Press Ctrl+C to exit\n";
Expand Down
Loading
Loading