pubnub / pubnub
This is the official PubNub PHP SDK repository.
Installs: 3 156 748
Dependents: 22
Suggesters: 1
Security: 1
Stars: 128
Watchers: 42
Forks: 137
Open Issues: 9
Requires
- php: >=8.0
- psr/log: ^1.1|^2.0|^3.0
- rmccue/requests: ^2.0
Requires (Dev)
- behat/behat: ^3.14
- monolog/monolog: ^2.9 || ^3.0
- phpstan/phpstan: ^1.8
- phpunit/phpunit: ^9.5
- squizlabs/php_codesniffer: ^3.7
- dev-master
- 7.1.0
- 7.0.2
- 7.0.1
- 7.0.0
- 6.3.0
- 6.2.1
- 6.2.0
- 6.1.3
- 6.1.2
- 6.1.1
- 6.1.0
- 6.0.1
- 6.0.0
- 5.1.0
- 5.0.0
- 4.7.0
- 4.6.0
- 4.5.0
- 4.4.0
- 4.3.0
- 4.2.0
- 4.1.7
- 4.1.5
- 4.1.4
- 4.1.3
- 4.1.2
- 4.1.1
- 4.1.0
- 4.0.0
- 4.0.0-beta.3
- 4.0.0-beta.2
- 4.0.0-beta
- 4.0.0-alpha
- 3.8.3
- 3.8.1
- 3.8.0
- v3.7.9
- v3.7.8
- v3.7.7
- v3.7.6
- v3.7.5
- v3.7.4
- v3.7.3
- v3.7.2
- v3.7.1
- v3.7.0
- v3.6.3
- v3.6.2
- v3.6.1
- v3.6.0
- v3.5.4
- v3.5.3
- v3.5.2
- 3.5
- dev-fix/typing-of-grant-token
- dev-fetat/message-persistence
- dev-chore/update-compatibility-info
- dev-feat/crypto_module
- dev-ghatest
- dev-feature/add-space-id-and-message-type
- dev-feature/GrantToken-behat-tests
- dev-feature/access-management-revoke-token
- dev-metadata-changes
- dev-composer-json-fix
- dev-develop
- dev-JeffGreen7-sourceclr
- dev-edge
- dev-master_3x
This package is auto-updated.
Last update: 2024-11-20 08:58:12 UTC
README
This is the official PubNub PHP SDK repository.
PubNub takes care of the infrastructure and APIs needed for the realtime communication layer of your application. Work on your app's logic and let PubNub handle sending and receiving data across the world in less than 100ms.
The SDK supports PHP 7.4 and 8.x.
Get keys
You will need the publish and subscribe keys to authenticate your app. Get your keys from the Admin Portal.
Configure PubNub
-
Integrate the PHP SDK into your project:
-
Without composer
-
Clone the following repository:
git clone https://github.com/pubnub/php.git ./pubnub-php
-
Copy the
src
folder to your project. -
Include
autoloader.php
file in your project:require_once('src/autoloader.php');
-
Download dependency
monolog
from https://github.com/Seldaek/monolog and copy themonolog
folder from thesrc
folder to thesrc
folder of your project. -
Download dependency
psr/Log
from https://github.com/php-fig/log/tree/master and copy thepsr
folder to thesrc
folder of your project. -
Download dependency
rmccue
from https://github.com/WordPress/Requests and copy theRequests
folder and the fileRequests.php
from thelibrary
folder to thesrc
folder of your project.
-
-
With composer
-
Add the PubNub package to your
composer.json
file:{ "require": { <!-- include the latest version from the badge at the top --> "pubnub/pubnub": "7.1.0" } }
-
Run
composer install --no-dev
from the command line. This installs the PubNub PHP SDK and all its dependencies in thevendor
folder of the project. -
Include
autoload.php
file in your project:require_once('vendor/autoload.php');
-
-
-
Configure your keys:
$pnconf = new PNConfiguration(); $pubnub = new PubNub($pnconf); $pnconf->setSubscribeKey("mySubscribeKey"); $pnconf->setPublishKey("myPublishKey"); $pnconf->setUserId("ReplaceWithYourClientIdentifier");
Add event listeners
class MySubscribeCallback extends SubscribeCallback { function status($pubnub, $status) { if ($status->getCategory() === PNStatusCategory::PNUnexpectedDisconnectCategory) { // This event happens when radio / connectivity is lost } else if ($status->getCategory() === PNStatusCategory::PNConnectedCategory){ // Connect event. You can do stuff like publish, and know you'll get it // Or just use the connected event to confirm you are subscribed for // UI / internal notifications, etc } else if ($status->getCategory() === PNStatusCategory::PNDecryptionErrorCategory){ // Handle message decryption error. Probably client configured to // encrypt messages and on live data feed it received plain text. } } function message($pubnub, $message){ // Handle new message stored in message.message } function presence($pubnub, $presence){ // handle incoming presence data } } $subscribeCallback = new MySubscribeCallback(); $pubnub->addListener($subscribeCallback);
Publish/subscribe
$pubnub->subscribe() ->channels("hello_world") ->execute(); $pubnub->publish() ->channel("hello_world") ->message("Hello PubNub!") ->sync();
Documentation
Support
If you need help or have a general question, contact support@pubnub.com.