PHP Chat Program Using Open Source PubNub API

Published: 13 June 2011
on channel: Stephen Blum
5,058
10

SOURCE CODE: https://github.com/pubnub/pubnub-api/...

The PHP program file is 70 lines and is easy to read through. This is a great demonstration of using PubNub PHP Client API. PHP Chat program was simple to build. Starting with STDIN to capture the user's screen name. Following with a connection to the PubNub server using the $pubnub->subscribe() PHP API function.

We are using pcntl_fork() command to let our subscribe process run as a child. All incoming data gets printed to the same STDOUT and we are not blocked by waiting for a message. We can send messages even while the subscribe process is listening.

To capture User Input we block on input in the parent process using trim(fgets(STDIN)). When the user presses Enter, a tick forward in the code captures typed text on the screen and allows us to execute the $pubnub->publish() api call. This sends the message to any connected users listening at the same time.

PHP Command Line Interface (CLI) and Server Push API Walkthrough pubnub.com/​blog/​php-push-api-walkthrough will bridge the gap between your JavaScript Browser (like Firefox) and PHP on the command line or in your server application using Apache, NginX, lighttpd and more.

Visit the home website of PubNub at pubnub.com to use our JavaScript API which works with iPhone and iPads!


On this page of the site you can watch the video online PHP Chat Program Using Open Source PubNub API with a duration of hours minute second in good quality, which was uploaded by the user Stephen Blum 13 June 2011, share the link with friends and acquaintances, this video has already been watched 5,058 times on youtube and it was liked by 10 viewers. Enjoy your viewing!