XML
  Home arrow XML arrow Page 2 - Building an AJAX-Based Chat: The Barebones...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
XML

Building an AJAX-Based Chat: The Barebones Structure
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 36
    2005-11-08

    Table of Contents:
  • Building an AJAX-Based Chat: The Barebones Structure
  • Defining the application’s core logic: working with requester objects
  • Sending chat messages: defining the “sendMessage()” function
  • Checking for the progress of sender requests: defining the “senderStatusChecker()” function
  • Updating the display of messages: coding the “displayChatData()” function

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    More XML Articles
    More By Alejandro Gervasio


       · The capabilities of AJAX for developing Web applications that resemble desktop...
       · The method you are using for AJAX really limits your audience since you are using...
       · Hello,Thank you for commenting on the article. With reference to your opinion,...
       · Well written. Thank you.
       · Thank you for the kind comments on the article. Very much...
       · Hi. Your chat seems to be interesting though i didn't manage to put it working...
       · Hello Paulo,Thank you for commenting on my article. I've tested my chat...
       · Thanks for the great articles, I'm in the process of designing a user-to-user chat...
       · Thank you for your kind comments on my articles. I'm really glad to know that you're...
       · HiIt is very interesting and excellent. I am going to implement based on your...
       · Hello Upen,Thank you for the compliments on my AJAX article; they’re really...
       · Hai I am also thinking to implementing .do you give your code.I am also help...
       · Thank you for commenting on my AJAX article. Concerning your question, you can...
     

    XML ARTICLES

    - 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
    - Simple Web Syndication with RSS 2.0
    - Java UI Design with an IDE







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek