Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    Dec 2007
    Posts
    41
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Is this most efficient?

    Hey,

    I have an ajax script for a chat room but wanted some one who knows ajax to tell me if there is a way i can increase its efficientcy if at all...as it calls a php script all the time to check for new messages - was wondering if my method of calls can be improved?

    Here is my script:

    *I completely self taught from researching so im sure theres bad habits in this and im sure theres a better way if you could please help it would be great!*

    Code:
    function createRequestObject() {
        var ro;
        var browser = navigator.appName;
        if(browser == "Microsoft Internet Explorer"){
            ro = new ActiveXObject("Microsoft.XMLHTTP");
        }else{
            ro = new XMLHttpRequest();
        }
        return ro;
    }
    
    var http = createRequestObject();
    
    function sndReq() {
        http.open('get', 'chatcheck.php');
        http.onreadystatechange = handleResponse;
        http.send(null);
    }
    
    function handleResponse() {
        if(http.readyState == 4){
            var response = http.responseText;
            var update = new Array();
            if(response == '1') {
                updateShouts();
            }
        }
    }
    
       
       
       
    function sendMessage()
    {
         $.ajax({
              type: "POST",
              url: "chatpost.php",
              async: false,
              data: "message="+$("#myMessage").val(),
           
          });
          $("#myMessage").val("");
          return false;
    }
    
    
    
    function updateShouts(){
        $('#chatArea').load('chat.php');
    }
    setInterval( "sndReq()", 1000 );

  • #2
    New Coder
    Join Date
    Dec 2007
    Posts
    41
    Thanks
    2
    Thanked 0 Times in 0 Posts
    >.> Hope this aint a sign this site is dead and just spammed alot..

  • #3
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    Well, first you posted in Ajax and Design, but your code relies on jQuery, so you might get some help there.

    I hate jQuery's .load. I find it's way too inefficient, especially for chat. There's no reason to reload every chat message you loaded in the last screen when there's only a few messages that are new.

    Instead, I use my PHP to return all chat messages from after a specific microsecond timestamp. Then I use javascript to document.createElement("div") or whatever model you use to build your chat document section and build it using javascript that way. Then I'm only ever making the browser render the newest stuff and not all the same stuff plus one or 2 new chat messages (or worse, none).

    Other than that, you may be able to attain efficiencies on the server side. And then there's also the COMET model that doesn't require polling the server when there's no new messages.

    Hope that helps. No, this board is not dead. It's very very very active.

  • #4
    New Coder
    Join Date
    Dec 2007
    Posts
    41
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Okay most of what you said is beyond my understanding xD

    My script is just me pulling bits from all over the place and putting it together :P I thought it was PHP that pulls the chat logs out not AJAX?

  • #5
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    1 user generates 1 request every second. Multiply that by 10 users that is 10 requests. Your server will get pounded as your load increases. Hope you have the bandwidth.
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #6
    New Coder
    Join Date
    Dec 2007
    Posts
    41
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Isnt that the only way ajax works though ?

  • #7
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #8
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,380
    Thanks
    11
    Thanked 592 Times in 572 Posts
    comet requires the server to leave a connection open. you can likely serve more messages (depending on your hosting package) using repetitive pings. if you can respond to a ping in 150ms, you can serve 8X the number of folks with pings than with comet. however, if your bounce time is over half a second, comet is going to be more satisfying to the user.



    the future is p2p though, that's going to save your server the most bandwidth, compared to any server-proxy config.

    http://dev.w3.org/html5/websockets/
    Last edited by rnd me; 08-28-2010 at 07:26 AM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%

  • #9
    New Coder
    Join Date
    Dec 2007
    Posts
    41
    Thanks
    2
    Thanked 0 Times in 0 Posts
    This comet is confusing its like a whole new language


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •