Home arrow XML arrow Page 2 - Building an AJAX-Based Chat: The Barebones Structure
XML

Building an AJAX-Based Chat: The Barebones Structure


Chat programs are common on the web these days. HTTP-based versions were often built as Java applets, but today, developers have a wider range of options when building chat programs. This article gets you started with building one that uses AJAX as the workhorse for sending out http requests without the need for page reloading.

Author Info:
By: Alejandro Gervasio
Rating: 5 stars5 stars5 stars5 stars5 stars / 43
November 08, 2005
TABLE OF CONTENTS:
  1. · Building an AJAX-Based Chat: The Barebones Structure
  2. · Defining the application’s core logic: working with requester objects
  3. · Sending chat messages: defining the “sendMessage()” function
  4. · Checking for the progress of sender requests: defining the “senderStatusChecker()” function
  5. · Updating the display of messages: coding the “displayChatData()” function

print this article
SEARCH DEVARTICLES

Building an AJAX-Based Chat: The Barebones Structure - Defining the application’s core logic: working with requester objects
(Page 2 of 5 )

Before I start defining the core functions that work with requester objects, let’s pause for a moment and have a look at the basic appearance of the chat application, depicted in the following screenshot:

 

In the simplest terms, the chat application that I plan to develop is based on common sense rather than on complicated programming logic. Due to the limitations imposed by XMLHtttpRequest objects (or an ActiveX control for Internet Explorer), which can handle only one request at a time, I’ll use two independent requester objects, in order to get the application to work. According to this, the first object will be responsible for pushing user messages into a message stack, stored in a simple MySQL database table, while the second object will be tasked with pulling a given number of messages from the database table, to be displayed on a message containing section.

What comes next, though? Since I’ll need to handle two separate objects for handling http requests across the application, the first step in building the chat will be defining an object factory function, which will allow the instantiation of as many requester objects as required. The pertinent function, not surprisingly called “getXMLHttpRequestObject()”, looks like this:

function getXMLHttpRequestObject(){
    var xmlobj;
    // check for existing requests
    if(xmlobj!=null&&xmlobj.readyState!=0&&xmlobj.readyState!=4){
        xmlobj.abort();
    }
    try{
        // instantiate object for Mozilla, Nestcape, etc.
        xmlobj=new XMLHttpRequest();
    }
    catch(e){
        try{
            // instantiate object for Internet Explorer
            xmlobj=new ActiveXObject('Microsoft.XMLHTTP');
        }
        catch(e){
            // Ajax is not supported by the browser
            xmlobj=null;
            return false;
        }
    }
    return xmlobj;
}

As shown in the function above, different requester objects will be instantiated and returned to calling code each time the function is invoked. Aside from coding the lines where actual object instantiation takes place, I’ve included some “try-catch” blocks for solving browser incompatibilities, particularly due to the absence of native support for XMLHttpRequest objects in Internet Explorer (hopefully this issue will be fixed in IE 7).

Now, I’ve defined a function which allows you to create the two requester objects required for sending out and getting chat messages. Since the chat application can be divided into two different programming modules –- sender and receiver -- where the first one will insert new user messages into a MySQL database table, and the second one will fetch messages for being displayed on the browser, let’s focus our attention on developing the functions that comprise the first module, for sending and stacking messages within the database table. Just click below and keep on reading.


blog comments powered by Disqus
XML ARTICLES

- Open XML Finally Supported by MS Office
- XML Features Added to Two Systems
- Using Regions with XSL Formatting Objects
- Using XSL Formatting Objects
- More Schematron Features
- Schematron Patterns and Validation
- Using Schematron
- Datatypes and More in RELAX NG
- Providing Options in RELAX NG
- An Introduction to RELAX NG
- Path, Predicates, and XQuery
- Using Predicates with XQuery
- Navigating Input Documents Using Paths
- XML Basics
- Introduction to XPath

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials