XML
  Home arrow XML arrow Page 3 - 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  
Dedicated Servers  
Actuate Whitepapers 
VeriSign Whitepapers 
IBM® developerWorks 
Sun Developer Network 
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: 4 stars4 stars4 stars4 stars4 stars / 31
    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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Building an AJAX-Based Chat: The Barebones Structure - Sending chat messages: defining the “sendMessage()” function


    (Page 3 of 5 )

    As you know, a typical chat application provides users with the ability to send out messages through a simple form, and then display them on a generic containing section where all online users communicate with each other. For this reason, I’ll begin developing the “sender” module of the application, by defining the “sendMessage()” function. It uses a “sender” XMLHttpRequest object for triggering a post request to a given file, and passes into it the user’s nickname, together with the corresponding message. Here’s what it looks like:

    function sendMessage(){
        var user='<?php echo $user?>';
        var message=document.getElementsByTagName('form')[0].elements
    [0].value;
        if(message.length>100){message=message.substring(0,100)};
        // open socket connection
        senderXMLHttpObj.open('POST','sendchatdata.php',true);
        // set form http header
        senderXMLHttpObj.setRequestHeader('Content-
    Type','application/x-www-form-urlencoded');
        senderXMLHttpObj.send('user='+user+'&message='+message);
        senderXMLHttpObj.onreadystatechange=senderStatusChecker;
    }

    In the simplest sense, the above function makes a post request to the “sendchatdata.php” file, and sends as parameters the pair of “user&message” variables, for pushing directly into the database table. Of course, there are a few things worth noting here. As you may have guessed, the backend of the chat will sit on a simple MySQL database, which will be accessed with PHP. But fear not. Considering the ease of querying the database, you can use another RDBMS (for example MS Access) and work with ASP.

    By returning to the function’s code, you can see that I use a “senderXMLHttpRequest” object for sending the request header, conjunctly with the pair of parameters mentioned above. For the moment, I’m assuming that this object has been previously instantiated by the “getXMLHttpRequestObject()” function, so this will be discussed later, when I explain the set of functions that initialize the chat.

    Notice right at the very beginning of the function how the lengths of messages are checked in, by limiting the amount of characters to 100, as well as how the user’s nickname is obtained. The line listed below:

    var user='<?php echo $user?>';

    demonstrates how to populate the “user” JavaScript variable with the contents of the “$user” PHP variable. I’ve deliberately done this in order to use PHP sessions for registering the nickname chosen by the user and for using it during the chat session. Obviously, the entire registration process will be done through a simple login page, which will be coded at a later time. For the moment, after the message has been sent by using a text input box, the “senderStatusChecker()” function is tied up to the “onreadystatechange” handler, so it’s possible to check for the progress of the request and insert the data into the database table.

    Since “senderXMLHttpRequest” objects are checked by the “senderStatusChecker()” function, the next step in developing the chat is looking at its corresponding definition.

    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

    - 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
    - UI Design with Java and XML Toolkits
    - Displaying ADO Retrieved Data with XML Islan...
    - Widget Walkthrough
    - Introduction to Widgets
    - The Why and How of XML Data Islands
    - Creating an XUL App Installer
    - Overlays in XUL
    - Skinning Your Custom XUL Applications







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway