This means JavaScript Browsers can push messages to PHP servers. JavaScript Browsers can also push to PHP command line programs running in a terminal. All APIs are 100% Real Time.
This Walkthrough is meant to work on your laptop in a terminal/console! If you don't have a terminal/console, then read the JavaScript Push API.
Ruby, JavaScript and C# Push APIs are available on GitHub. Download PubNub API on GitHub
PHP developers can push messages to JavaScript Browsers. Read PHP Push API Reference Tutorial for a condensed reference of API function calls.
Let's take a look at how developers can
create channels between PHP and JavaScript.
The most common usage pattern
for real time applications will be explained first.
A JavaScript Browser (like Firefox) will subscribe and listen for messages
with PUBNUB.subscribe().
PHP will then push messages with $pubnub.publish().
JavaScript
PUBNUB.subscribe( { channel : 'my_test_channel' }, function(message) { if ('some_text' in message) { alert(message.some_text); } } );
The above JavaScript is 100% Cross Browser Compatible. The code will listen for messages published on 'my_test_channel' channel. When a message is received, the JavaScript will validate if 'some_text' exists in the message object. If this attribute exists, then show an alert box!
Now use PHP to publish a message to invoke the JavaScript Alert box.
PHP
## Publish Messages To a JavaScript Browser $pubnub = new Pubnub( 'publish_key', 'subscribe_key' ); $pubnub->publish(array( 'channel' => 'my_test_channel', 'message' => array( 'some_text' => 'hello!' ) ));
This PHP code will send a message to a JavaScript Browser listening on 'my_test_channel' channel. When this PHP Code executes, a JavaScript Browser will receive the PHP array and show an alert message of 'hello!'.
This next example is uncommon, albeit powerful.
JavaScript has the capability to send messages to PHP.
PUBNUB.publish() in a JavaScript Browser will send real time
messages to a PHP Script.
This PHP Script will be listening with $pubnub.subscribe(). PHP can receive messages directly from a JavaScript Browser. Here is the PHP Code:
PHP Listens for a Message
$pubnub->subscribe(array( 'channel' => 'extra_cool_channel', 'callback' => function($message) { var_dump($message); return true; ## continue listening } ));
The above code makes PHP listen on 'extra_cool_channel' channel. Next we send messages from JavaScript to PHP. Note that this PHP code may be running in a terminal window.
JavaScript
PUBNUB.publish({ channel : 'extra_cool_channel', message : { 'some_var' : 'what up?' } });
When we execute this JavaScript code,
the PHP Script will receive
{ 'some_var' : 'what up?' } message.
The output of the PHP Script will look like:
PHP Output
array(1) { ["some_var"]=> string(8) "what up?" }
That's it. Pretty simple right? This code works without needing a web server! It will work on your laptop right now!
You need Publish and Subscribe Keys.
Publish and Subscribe Keys are needed for
every
The video demonstrates a CLI (Command Line Interface) PHP Chat Client.
It uses real time
PHP Chat Program Using Open Source Pubnub API
The link below will take you to the PHP Push API Reference Page with a full list of available API calls. PHP Push API Reference Tutorial