XML
  Home arrow XML arrow Page 5 - 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 - Updating the display of messages: coding the “displayChatData()” function


    (Page 5 of 5 )

    As I mentioned before, the list of the messages being displayed on the chat page needs to be refreshed each time a new message is added to the database table. Keeping this condition in mind, the chat application uses the “displayChatData()” function, in order to perform the message updating process. Here is the definition for the pertinent function:

    function displayChatData(reqObj){
        // remove previous messages
        var mdiv=document.getElementById('messages');
        if(!mdiv){return};
        mdiv.innerHTML='';
        var messages=reqObj.responseText.split('|');
        // display messages
        for(var i=0;i<messages.length;i++){
            var p=document.createElement('p');
            p.appendChild(document.createTextNode(messages
    [(messages.length-1)-i]));
            mdiv.appendChild(p);
        }
    }

    By analyzing the source code for the function above, it’s pretty easy to understand its operation. First, it removes all the messages previously displayed and then obtains a new list. The following fragment of code illustrates these processes:

    // remove previous messages
    var mdiv=document.getElementById('messages');
    if(!mdiv){return};
    mdiv.innerHTML='';
    var messages=reqObj.responseText.split('|');

    As you can see, the function cleans up a “messages” <div> containing element by resetting its “innerHTML” property. After all the messages have been removed from the container, the new list of messages is obtained by reading the value for the “responseText” property, belonging to the requester object passed in as a parameter.

    Since all the messages will be fetched from the database first, and then delimited by a pipe (“|”) character, for transmitting back to the client, the below expression:

    var messages=reqObj.responseText.split('|');

    stores the messages in the “messages” array. This is handy for processing and displaying with a simple loop, like this:

    for(var i=0;i<messages.length;i++){
        var p=document.createElement('p');
        p.appendChild(document.createTextNode(messages
    [(messages.length-1)-i]));
        mdiv.appendChild(p);
    }

    As you may have guessed, each message will be placed within the container <div> as a paragraph element, and displayed in descending order, assuming that the last inserted message will be shown at the bottom of the containing <div>.

    By this point, I’ve provided you with all the JavaScript functions that compose the “sender” module for the chat application. Of course, the functions that you just saw seem rather like isolated pieces, unconnected to the whole application due to the fact that the remaining “receiver” module hasn’t been coded yet. Moreover, there is much of the server-side processing left to be developed, including insertion and retrieval of messages. So, bear with me and wait for the next part of this series, where I’ll be writing the rest of the AJAX-based chat.

    Wrapping up

    Over the first part of this series, I’ve demonstrated how to build the barebones of a web-based chat system, by utilizing AJAX as the trusty workhorse for sending, retrieving and updating user messages, without the need to appeal to “refresh” meta tags, and certainly keeping all the programming requirements under the scope of JavaScript and PHP.

    However, exciting things are just around the corner. As I said before, in the next tutorial, I’ll write the “receiver” JavaScript module, useful for fetching messages from the database table, together with some additional functions for building the layout of the chat page. Is this too much for you? I don’t think so. See you in the next part!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · 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 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek