From 36aafd534bb2e3e55aa4fafc9de49bb3d1210cf8 Mon Sep 17 00:00:00 2001 From: Mateusz Wiktor Date: Thu, 6 Nov 2025 11:27:06 +0100 Subject: [PATCH 1/2] Added getting started snippets --- examples/basic_usage/getting_started.php | 134 +++++++++++++++++++++++ examples/basic_usage/publish.php | 6 +- examples/basic_usage/subscribe.php | 78 +++++++++++++ 3 files changed, 216 insertions(+), 2 deletions(-) create mode 100644 examples/basic_usage/getting_started.php create mode 100644 examples/basic_usage/subscribe.php diff --git a/examples/basic_usage/getting_started.php b/examples/basic_usage/getting_started.php new file mode 100644 index 0000000..f5d9ab6 --- /dev/null +++ b/examples/basic_usage/getting_started.php @@ -0,0 +1,134 @@ +setSubscribeKey(getenv("SUBSCRIBE_KEY") ?? "demo"); // Replace with your subscribe key + +// Set publish key (only required if publishing) +$pnConfiguration->setPublishKey(getenv("PUBLISH_KEY") ?? "demo"); // Replace with your publish key + +// Set UUID (required to connect) +$pnConfiguration->setUserId("php-sdk-user-" . uniqid()); + +// Set up cryptography for message encryption (optional) +// $pnConfiguration->setCryptoModule(CryptoModule::aesCbcCryptor("your-cipher-key", true)); + +// Configure connection timeout in seconds +$pnConfiguration->setConnectTimeout(10); + +// Create PubNub instance with the configured settings +$pubnub = new PubNub($pnConfiguration); +// snippet.end + +// snippet.event_listeners +class MySubscribeCallback extends SubscribeCallback { + function status($pubnub, $status) { + if ($status->getCategory() === PNStatusCategory::PNUnexpectedDisconnectCategory) { + // This event happens when radio / connectivity is lost + echo "Unexpected disconnect - network may be down" . PHP_EOL; + } else if ($status->getCategory() === PNStatusCategory::PNConnectedCategory) { + // Connect event. You can do stuff like publish, and know you'll get it + echo "Connected to PubNub!" . PHP_EOL; + } else if ($status->getCategory() === PNStatusCategory::PNDecryptionErrorCategory) { + // Handle message decryption error + echo "Decryption error: " . $status->getException() . PHP_EOL; + } + } + + function message($pubnub, $message) { + // Handle new message stored in message.message + echo "Received message: " . json_encode($message->getMessage()) . PHP_EOL; + echo "Publisher: " . $message->getPublisher() . PHP_EOL; + echo "Channel: " . $message->getChannel() . PHP_EOL; + echo "Timetoken: " . $message->getTimetoken() . PHP_EOL; + } + + function presence($pubnub, $presence) { + // Handle incoming presence data + echo "Presence event: " . $presence->getEvent() . PHP_EOL; + echo "UUID: " . $presence->getUuid() . PHP_EOL; + echo "Channel: " . $presence->getChannel() . PHP_EOL; + echo "Occupancy: " . $presence->getOccupancy() . PHP_EOL; + } +} + +// Add listener +$subscribeCallback = new MySubscribeCallback(); +$pubnub->addListener($subscribeCallback); +// snippet.end + +// snippet.create_subscription +// Subscribe to a channel +$pubnub->subscribe() + ->channels("hello_world") + ->withPresence(true) // Optional: subscribe to presence events + ->execute(); +// snippet.end + +// snippet.publish_message +// Assuming $pubnub is already initialized + +try { + // Create message data + $messageData = [ + "text" => "Hello from PHP SDK!", + "timestamp" => time(), + "sender" => [ + "name" => "PHP Publisher", + "id" => $pnConfiguration->getUserId() + ] + ]; + + // Publish a message to a channel + $result = $pubnub->publish() + ->channel("hello_world") // Channel to publish to + ->message($messageData) // Message content + ->shouldStore(true) // Store in history + ->sync(); // Execute synchronously + + // Display success message + echo "Message published successfully!" . PHP_EOL; + echo "Timetoken: " . $result->getTimetoken() . PHP_EOL; + + // Convert timetoken to readable date + $timestamp = floor($result->getTimetoken() / 10000000); + $readableDate = date('Y-m-d H:i:s', $timestamp); + echo "Published at: " . $readableDate . PHP_EOL; + + // Display published message + echo PHP_EOL . "Published message: " . PHP_EOL; + echo json_encode($messageData, JSON_PRETTY_PRINT) . PHP_EOL; +} catch (PubNubServerException $exception) { + // Handle PubNub server-specific errors + echo "Error publishing message: " . $exception->getMessage() . PHP_EOL; + + if (method_exists($exception, 'getServerErrorMessage') && $exception->getServerErrorMessage()) { + echo "Server Error: " . $exception->getServerErrorMessage() . PHP_EOL; + } + + if (method_exists($exception, 'getStatusCode') && $exception->getStatusCode()) { + echo "Status Code: " . $exception->getStatusCode() . PHP_EOL; + } +} catch (PubNubException $exception) { + // Handle PubNub-specific errors + echo "PubNub Error: " . $exception->getMessage() . PHP_EOL; +} catch (Exception $exception) { + // Handle general exceptions + echo "Error: " . $exception->getMessage() . PHP_EOL; +} +// snippet.end + diff --git a/examples/basic_usage/publish.php b/examples/basic_usage/publish.php index c2bb7f4..d5c6f61 100644 --- a/examples/basic_usage/publish.php +++ b/examples/basic_usage/publish.php @@ -8,11 +8,12 @@ use PubNub\Exceptions\PubNubServerException; use PubNub\Exceptions\PubNubException; +// snippet.publish_complete // Create configuration with demo keys $pnConfig = new PNConfiguration(); $pnConfig->setSubscribeKey(getenv("SUBSCRIBE_KEY") ?? "demo"); $pnConfig->setPublishKey(getenv("PUBLISH_KEY") ?? "demo"); -$pnConfig->setUserId("php-publish-demo-user"); +$pnConfig->setUserId("php-publisher-demo"); // Initialize PubNub instance $pubnub = new PubNub($pnConfig); @@ -30,7 +31,7 @@ // Publish a message to a channel $result = $pubnub->publish() - ->channel("my_channel") // Channel to publish to + ->channel("hello_world") // Channel to publish to ->message($messageData) // Message content ->shouldStore(true) // Store in history ->ttl(15) // Time to live (hours) @@ -68,3 +69,4 @@ // Handle general exceptions echo "Error: " . $exception->getMessage() . PHP_EOL; } +// snippet.end diff --git a/examples/basic_usage/subscribe.php b/examples/basic_usage/subscribe.php new file mode 100644 index 0000000..7394680 --- /dev/null +++ b/examples/basic_usage/subscribe.php @@ -0,0 +1,78 @@ +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-subscriber"); + +// Set up cryptography for message encryption (optional) +// $pnConfiguration->setCryptoModule(CryptoModule::aesCbcCryptor("your-cipher-key", true)); + +// Configure connection timeout in seconds +$pnConfiguration->setConnectTimeout(10); + +// Create PubNub instance with the configured settings +$pubnub = new PubNub($pnConfiguration); +// snippet.end + +// snippet.event_listeners +class MySubscribeCallback extends SubscribeCallback { + function status($pubnub, $status) { + if ($status->getCategory() === PNStatusCategory::PNUnexpectedDisconnectCategory) { + // This event happens when radio / connectivity is lost + echo "Unexpected disconnect - network may be down" . PHP_EOL; + } else if ($status->getCategory() === PNStatusCategory::PNConnectedCategory) { + // Connect event. You can do stuff like publish, and know you'll get it + echo "Connected to PubNub!" . PHP_EOL; + } else if ($status->getCategory() === PNStatusCategory::PNDecryptionErrorCategory) { + // Handle message decryption error + echo "Decryption error: " . $status->getException() . PHP_EOL; + } + } + + function message($pubnub, $message) { + // Handle new message stored in message.message + echo "Received message: " . json_encode($message->getMessage()) . PHP_EOL; + echo "Publisher: " . $message->getPublisher() . PHP_EOL; + echo "Channel: " . $message->getChannel() . PHP_EOL; + echo "Timetoken: " . $message->getTimetoken() . PHP_EOL; + } + + function presence($pubnub, $presence) { + // Handle incoming presence data + echo "Presence event: " . $presence->getEvent() . PHP_EOL; + echo "UUID: " . $presence->getUuid() . PHP_EOL; + echo "Channel: " . $presence->getChannel() . PHP_EOL; + echo "Occupancy: " . $presence->getOccupancy() . PHP_EOL; + } +} + +// Add listener +$subscribeCallback = new MySubscribeCallback(); +$pubnub->addListener($subscribeCallback); +// snippet.end + +// snippet.create_subscription +// Subscribe to a channel, this will block execution +$pubnub->subscribe() + ->channels("hello_world") + ->withPresence(true) // Optional: subscribe to presence events + ->execute(); +// snippet.end + From 4a471b99ba8e3a14d681d4e0925e28f6fe5ec68f Mon Sep 17 00:00:00 2001 From: "PUBNUB\\jakub.grzesiowski" Date: Thu, 6 Nov 2025 12:41:50 +0100 Subject: [PATCH 2/2] Linter tweaks --- examples/basic_usage/getting_started.php | 23 +++++++++++++++-------- examples/basic_usage/subscribe.php | 23 +++++++++++++++-------- 2 files changed, 30 insertions(+), 16 deletions(-) diff --git a/examples/basic_usage/getting_started.php b/examples/basic_usage/getting_started.php index f5d9ab6..ed9c352 100644 --- a/examples/basic_usage/getting_started.php +++ b/examples/basic_usage/getting_started.php @@ -1,4 +1,8 @@ getCategory() === PNStatusCategory::PNUnexpectedDisconnectCategory) { // This event happens when radio / connectivity is lost echo "Unexpected disconnect - network may be down" . PHP_EOL; - } else if ($status->getCategory() === PNStatusCategory::PNConnectedCategory) { + } elseif ($status->getCategory() === PNStatusCategory::PNConnectedCategory) { // Connect event. You can do stuff like publish, and know you'll get it echo "Connected to PubNub!" . PHP_EOL; - } else if ($status->getCategory() === PNStatusCategory::PNDecryptionErrorCategory) { + } elseif ($status->getCategory() === PNStatusCategory::PNDecryptionErrorCategory) { // Handle message decryption error echo "Decryption error: " . $status->getException() . PHP_EOL; } - } + } - function message($pubnub, $message) { + public function message($pubnub, $message) + { // Handle new message stored in message.message echo "Received message: " . json_encode($message->getMessage()) . PHP_EOL; echo "Publisher: " . $message->getPublisher() . PHP_EOL; @@ -57,7 +64,8 @@ function message($pubnub, $message) { echo "Timetoken: " . $message->getTimetoken() . PHP_EOL; } - function presence($pubnub, $presence) { + public function presence($pubnub, $presence) + { // Handle incoming presence data echo "Presence event: " . $presence->getEvent() . PHP_EOL; echo "UUID: " . $presence->getUuid() . PHP_EOL; @@ -131,4 +139,3 @@ function presence($pubnub, $presence) { echo "Error: " . $exception->getMessage() . PHP_EOL; } // snippet.end - diff --git a/examples/basic_usage/subscribe.php b/examples/basic_usage/subscribe.php index 7394680..cf8216c 100644 --- a/examples/basic_usage/subscribe.php +++ b/examples/basic_usage/subscribe.php @@ -1,4 +1,8 @@ getCategory() === PNStatusCategory::PNUnexpectedDisconnectCategory) { // This event happens when radio / connectivity is lost echo "Unexpected disconnect - network may be down" . PHP_EOL; - } else if ($status->getCategory() === PNStatusCategory::PNConnectedCategory) { + } elseif ($status->getCategory() === PNStatusCategory::PNConnectedCategory) { // Connect event. You can do stuff like publish, and know you'll get it echo "Connected to PubNub!" . PHP_EOL; - } else if ($status->getCategory() === PNStatusCategory::PNDecryptionErrorCategory) { + } elseif ($status->getCategory() === PNStatusCategory::PNDecryptionErrorCategory) { // Handle message decryption error echo "Decryption error: " . $status->getException() . PHP_EOL; } - } + } - function message($pubnub, $message) { + public function message($pubnub, $message) + { // Handle new message stored in message.message echo "Received message: " . json_encode($message->getMessage()) . PHP_EOL; echo "Publisher: " . $message->getPublisher() . PHP_EOL; @@ -54,7 +61,8 @@ function message($pubnub, $message) { echo "Timetoken: " . $message->getTimetoken() . PHP_EOL; } - function presence($pubnub, $presence) { + public function presence($pubnub, $presence) + { // Handle incoming presence data echo "Presence event: " . $presence->getEvent() . PHP_EOL; echo "UUID: " . $presence->getUuid() . PHP_EOL; @@ -75,4 +83,3 @@ function presence($pubnub, $presence) { ->withPresence(true) // Optional: subscribe to presence events ->execute(); // snippet.end -