Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-14-2010, 10:59 PM   PM User | #1
SirChick
New Coder

 
Join Date: Dec 2007
Posts: 41
Thanks: 2
Thanked 0 Times in 0 Posts
SirChick is an unknown quantity at this point
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 );
SirChick is offline   Reply With Quote
Old 08-15-2010, 05:15 PM   PM User | #2
SirChick
New Coder

 
Join Date: Dec 2007
Posts: 41
Thanks: 2
Thanked 0 Times in 0 Posts
SirChick is an unknown quantity at this point
>.> Hope this aint a sign this site is dead and just spammed alot..
SirChick is offline   Reply With Quote
Old 08-16-2010, 06:49 AM   PM User | #3
Beagle
Senior Coder

 
Join Date: Jul 2005
Location: New York, NY
Posts: 1,084
Thanks: 4
Thanked 19 Times in 19 Posts
Beagle is an unknown quantity at this point
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.
Beagle is offline   Reply With Quote
Old 08-16-2010, 04:56 PM   PM User | #4
SirChick
New Coder

 
Join Date: Dec 2007
Posts: 41
Thanks: 2
Thanked 0 Times in 0 Posts
SirChick is an unknown quantity at this point
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?
SirChick is offline   Reply With Quote
Old 08-27-2010, 09:06 PM   PM User | #5
A1ien51
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
A1ien51 will become famous soon enough
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]
A1ien51 is offline   Reply With Quote
Old 08-27-2010, 09:46 PM   PM User | #6
SirChick
New Coder

 
Join Date: Dec 2007
Posts: 41
Thanks: 2
Thanked 0 Times in 0 Posts
SirChick is an unknown quantity at this point
Isnt that the only way ajax works though ?
SirChick is offline   Reply With Quote
Old 08-27-2010, 10:55 PM   PM User | #7
A1ien51
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
A1ien51 will become famous soon enough
http://en.wikipedia.org/wiki/Comet_%28programming%29
__________________
Tech Author [Ajax In Action, JavaScript: Visual Blueprint]
A1ien51 is offline   Reply With Quote
Old 08-28-2010, 07:23 AM   PM User | #8
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,468
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
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/
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%

Last edited by rnd me; 08-28-2010 at 07:26 AM..
rnd me is offline   Reply With Quote
Old 10-06-2010, 11:25 PM   PM User | #9
SirChick
New Coder

 
Join Date: Dec 2007
Posts: 41
Thanks: 2
Thanked 0 Times in 0 Posts
SirChick is an unknown quantity at this point
This comet is confusing its like a whole new language
SirChick is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:45 AM.


Advertisement
Log in to turn off these ads.