PHP Push API Walkthrough

3 min read Joe 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

Top Trends to Engage Your App Users
InsightsSep 27, 20224 min read

Top Trends to Engage Your App Users

Take a look at the top trends that are the most effective in attracting customers and reducing churn, and how you can incorporate...

PubNub Staff

PubNub Staff

Comparing Game Engines: Unity vs Unreal vs the Rest
Real-Time Gaming BlogSep 21, 20225 min read

Comparing Game Engines: Unity vs Unreal vs the Rest

Comparing the major game engines: Unity vs Unreal Engine vs Corona SDK vs GameMaker Studio, including the benefits and cons of...

PubNub Staff

PubNub Staff

Python Socket Programming: Client, Server and Peer Libraries
BuildSep 21, 20226 min read

Python Socket Programming: Client, Server and Peer Libraries

Sockets (aka socket programming) enable programs to send and receive data, bi-directionally, at any given moment. This tutorial...

PubNub Staff

PubNub Staff