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
141 changes: 141 additions & 0 deletions examples/basic_usage/getting_started.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<?php
// snippet.hide
// Ignoring example because of the "no side effects" rule
// phpcs:ignoreFile
// snippet.show

// This file contains code snippets for the Getting Started guide
// Include Composer autoloader (adjust path if needed)
require_once 'vendor/autoload.php';

use PubNub\PNConfiguration;
use PubNub\PubNub;
use PubNub\Callbacks\SubscribeCallback;
use PubNub\Enums\PNStatusCategory;
use PubNub\Exceptions\PubNubServerException;
use PubNub\Exceptions\PubNubException;

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

// Set subscribe key (required)
$pnConfiguration->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
{
public 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;
} 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;
} elseif ($status->getCategory() === PNStatusCategory::PNDecryptionErrorCategory) {
// Handle message decryption error
echo "Decryption error: " . $status->getException() . PHP_EOL;
}
}

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;
echo "Channel: " . $message->getChannel() . PHP_EOL;
echo "Timetoken: " . $message->getTimetoken() . PHP_EOL;
}

public 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
6 changes: 4 additions & 2 deletions examples/basic_usage/publish.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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)
Expand Down Expand Up @@ -68,3 +69,4 @@
// Handle general exceptions
echo "Error: " . $exception->getMessage() . PHP_EOL;
}
// snippet.end
85 changes: 85 additions & 0 deletions examples/basic_usage/subscribe.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php
// snippet.hide
// Ignoring example because of the "no side effects" rule
// phpcs:ignoreFile
// snippet.show

// Include Composer autoloader (adjust path if needed)
require_once 'vendor/autoload.php';

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

// snippet.initialize_pubnub
// 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-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
{
public 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;
} 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;
} elseif ($status->getCategory() === PNStatusCategory::PNDecryptionErrorCategory) {
// Handle message decryption error
echo "Decryption error: " . $status->getException() . PHP_EOL;
}
}

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;
echo "Channel: " . $message->getChannel() . PHP_EOL;
echo "Timetoken: " . $message->getTimetoken() . PHP_EOL;
}

public 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
Loading