PHP Client

The PHP scripting engine typically runs as a loadable module in a web server process. PHP scripts are embedded within HTML content and executed dynamically as the web server prepares a page on behalf of a web client. To provide a more interactive experience and minimize bandwidth consumption, a web application often combines PHP's server-side scripting capabilities with code that runs in the web browser. One technique, commonly referred to as Ajax, involves asynchronous JavaScript programs executing within the browser and exchanging XML messages with a web server. Rather than reloading the entire page to refresh its content, the JavaScript code is able to update individual elements of the page's presentation in the browser.

In the discussion of the chat application's design, we explained why the web-based clients must poll the server for new chat events. Since PHP scripts are only executed in the context of HTTP requests, a simplistic implementation of a chat client would require the user to manually refresh the web page in order to view the latest chat messages. In other words, the PHP script only polls for new updates whenever the user actively requests it. A better solution (the one used by our chat client) uses Ajax to check for new events without requiring any action by the user.

In our implementation, JavaScript code in the browser communicates at regular intervals with a PHP script in the web server. Each request prompts the PHP script to issue a synchronous Ice invocation to the chat server to check for new updates. The JavaScript program translates the results into local modifications of the web page. For example, shown below is an abbreviated version of the JavaScript code that polls for new chat events:

getUpdates:function()
{
    var params = new Hash();
    params.set('action','getUpdates');
    var opts =
    {
        contentType:'application/x-www-form-urlencoded',
        method:'post',
        encoding:'UTF-8',
        parameters:params,
        onComplete:function(transport)
        {
            var response = transport.responseText.evalJSON(true);
            for(var i = 0; i < response.length; i++)
            {
                switch(response[i].jsontype)
                {
                    case "PollingChat_MessageEvent":
                        if(response[i].name != _username)
                        {
                            var message = new Element('div').update(
                                "<div>" + formatDate(response[i].timestamp) + " - <"
                                + response[i].name + "> " + response[i].message
                                + "</div>");
                            addMessage(message);
                        }
                        break;
                    ...
                }
            }
        }
    }
}

The PHP script running in the web server formats the chat updates using JSON. The following PHP code summarizes the script's activities:

$chatsessionfactory = $this->_communicator->propertyToProxy("PollingChatSessionFactory");
$chatsessionfactory =
    $proxy->ice_uncheckedCast("::PollingChat::PollingChatSessionFactory");
$chatsession = $chatsessionfactory->create($username, $password);
$updates = $chatsession->getUpdates();
$data = array();
$length = count($updates);
for($i = 0; $i < $length; $i++)
{
    $update = $updates[$i];
    $update->jsontype = get_class($update);
    $data[] = $update;
}
$json = new Services_JSON();
print($json->encode($data));

If you would like to see the PHP client in action, you can use ZeroC's live chat server.