View Full Version : Is this most efficient?

08-14-2010, 10:59 PM

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!*

function createRequestObject() {
var ro;
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer"){
ro = new ActiveXObject("Microsoft.XMLHTTP");
ro = new XMLHttpRequest();
return ro;

var http = createRequestObject();

function sndReq() {
http.open('get', 'chatcheck.php');
http.onreadystatechange = handleResponse;

function handleResponse() {
if(http.readyState == 4){
var response = http.responseText;
var update = new Array();
if(response == '1') {

function sendMessage()
type: "POST",
url: "chatpost.php",
async: false,
data: "message="+$("#myMessage").val(),

return false;

function updateShouts(){
setInterval( "sndReq()", 1000 );

08-15-2010, 05:15 PM
>.> Hope this aint a sign this site is dead and just spammed alot..

08-16-2010, 06:49 AM
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.

08-16-2010, 04:56 PM
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?

08-27-2010, 09:06 PM
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.

08-27-2010, 09:46 PM
Isnt that the only way ajax works though ?

08-27-2010, 10:55 PM

rnd me
08-28-2010, 07:23 AM
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.


10-06-2010, 11:25 PM
This comet is confusing its like a whole new language :(