PHP Push API Walkthrough

3 min readJoe Hanson on Jul 20, 2010
Try PubNub Today

Free up to 1MM monthly messages. No credit card required.

Subscribe to our newsletter

By submitting this form, you are agreeing to our Terms and Conditions and Privacy Policy.

PubNub adds Publish and Subscribe for PHP developers. Developers can use Publish/Subscribe from a terminal or web server.

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

More from PubNub

Can Empathy Exist in the Metaverse
News May 16, 20221 min read

Can Empathy Exist in the Metaverse

A roundtable discussion led by PubNub’s COO, Casey Clegg, exploring the topics of what it means to be human in a virtual world.

PubNub Staff

PubNub Staff

How to Advance Telehealth and Virtual Care Technologies
News May 2, 20221 min read

How to Advance Telehealth and Virtual Care Technologies

Dr. Joe Kvedar, Chair of the Board for the American Telemedicine Association, joins our COO, Casey Clegg, to discuss why...

PubNub Staff

PubNub Staff

Another Step Towards Data Security: ISO-27001 Implementation
BuildMay 2, 20221 min read

Another Step Towards Data Security: ISO-27001 Implementation

Today, we are glad to announce that we are currently in the process of implementing ISO-27001 security standards.

PubNub Staff

PubNub Staff