PHP Push API Walkthrough

3 min read Jul 20, 2010

PubNub adds Publish and Subscribe for PHP developers. Developers can use Publish/Subscribe from a terminal or web server like Apache/NginX/lighttpd. PubNub PHP Push API provides real time Publish and Subscribe for PHP Developers.

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 walk through is intended to work on your laptop in a terminal/console. If you don’t have a terminal/console, then read the JavaScript Push API.

Our RubyJavaScript and C# Push APIs are available on GitHub. Download the 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.

JavaScript PHP Push API “Hello World”

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().


The above JavaScript is fully 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.


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!’.

Send Messages to PHP from JavaScript 

This next example is uncommon, but 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

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.


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

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 PubNub application. All the API keys are available in the Developer Portal here. Just sign up for an account and all the keys will be available.

PHP Command Line Chat Video

The video demonstrates a CLI (Command Line Interface) PHP Chat Client. It uses real time PubNub Publish and Subscribe Framework for PHP Developers.

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