XML
  Home arrow XML arrow Page 2 - Building an AJAX-Based Chat: Coding the Re...
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: Coding the Receiver Module
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 15
    2005-11-15

    Table of Contents:
  • Building an AJAX-Based Chat: Coding the Receiver Module
  • Retrieving messages from the database: defining the “getChatData()” function
  • Building the chat layout: writing the “createMessageBoard()” and “createMessageBox()” functions
  • Registering chat users: defining a simple login page
  • Putting the pieces together: building the whole chat page

  • 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: Coding the Receiver Module - Retrieving messages from the database: defining the “getChatData()” function


    (Page 2 of 5 )

    The whole idea for retrieving messages from the database is to request a PHP file that performs a SELECT query for extracting them, and then sends them back to the client for proper display. This entire process is carried out repetitively, at a given time interval. Obviously, the shorter the time interval, the more responsive will be the display of messages. With this concept in mind, here is the recursive definition for the “getChatData()” function:

    function getChatData(){
        receiverXMLHttpObj.open('GET','getchatdata.php',true);
        receiverXMLHttpObj.send(null);
        receiverXMLHttpObj.onreadystatechange=
    receiverStatusChecker;
        setTimeout('getChatData()',5*1000);
    }

    Even though this function is extremely simple, I believe you will agree that it is remarkably efficient. Essentially, it will fetch the “getchatdata.php” file each five seconds, and all the requests will be handled by the “receiverStatusCheker()” function. As you may guess, the PHP file is tasked with fetching a given number of messages to be displayed on the chat page.

    Now, let’s take a look at the “receiverStatusChecker()” function, in order to see how “receiver” requests are properly handled. Below is its source code:

    function receiverStatusChecker(){
        // if request is completed
        if(receiverXMLHttpObj.readyState==4){
            if(receiverXMLHttpObj.status==200){
     // if status == 200 display chat data
     displayChatData(receiverXMLHttpObj);
            }
            else{
                alert('Failed to get response :'+ receiverXMLHttpObj.statusText);
            }
        }
    }

    As you can see, the function above handles all the GET requests for getting messages from the server. After the list of messages has been obtained, it will be displayed on the chat page by using the “displayChatData()” function. As you recall, this function was explained in the first part of the series, and since it accepts a requester object as a parameter, it’s used by the respective “sender” and “receiver” objects.

    At this point, the chat application is capable of handling independently two requester objects for performing some clearly delineated tasks: retrieving and displaying messages from the server, and inserting new messages into the database each time a user submits a comment. See how the pieces are fitting together in the whole application?

    Now that you know how the chat is driven by two independent objects, the next step will be showing the functions that build the basic layout of the chat page. Thus, join me in the next section to find out how this is achieved.

    More XML Articles
    More By Alejandro Gervasio


       · Over this second article, the "receiver" module of the chat application is properly...
       · This is my first foray into AJAX and DOM, and I this tut was the best place to...
       · Thank you for your compliments on my AJAX tutorial. I'm really glad to know that...
       · Nice articleIsnt it bad pratice to call a function recursively without ever exiting...
       · Thank you for commenting on my article. Comming to your question, recursion can be...
       · Nice article,It would be great if they had a link to an example at the beginning...
       · Thank you for the kind comments on my AJAX tutorial. Of course, when applicable,...
       · Nice article & very interesting, since I have been looking for a thorough, detailed...
       · Hello Rasmus,I'm glad to know that my AJAX chat was what you were looking for....
       · I loved your article. It helped me understand AJAX more. BUT, this does not want...
       · Thank you so much for the kind comments on this tutorial. It's good to see it's been...
       · Nice article. I had just finished my own AJAX chat tutorial when I ran across yours....
       · I'd like to thank you for commenting on my AJAX-based chat tutorial, particulary if...
     

    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 5 Hosted by Hostway
    Stay green...Green IT