bPHP Posted April 21, 2010 Share Posted April 21, 2010 Hi! I have a code in ajax that is constantly calling a php function to check a database. Even though the php only sends information when it needs to (when the database was updated) if I see my page in firebug, I can see that it is constantly creating requests. How can I avoid this? Is there anyway to have it more clean? Thanks! Quote Link to comment Share on other sites More sharing options...
seventheyejosh Posted April 21, 2010 Share Posted April 21, 2010 Do you have your ajax call in a setInterval? Or do you setTimeout after your function and call the same function? Quote Link to comment Share on other sites More sharing options...
bPHP Posted April 21, 2010 Author Share Posted April 21, 2010 Do you have your ajax call in a setInterval? Or do you setTimeout after your function and call the same function? I setTimeout. It is something like this: mTimer = setTimeout('getChatText();',100); //Refresh chat text getChatText looks like this: function getChatText() { if (receiveReq.readyState == 4 || receiveReq.readyState == 0) { receiveReq.open("GET", 'chat.php', true); receiveReq.onreadystatechange = handleReceiveChat; receiveReq.send(null); } } Chat.php sends back a JSON string that my function handleReceiveChat processes appropriately. Quote Link to comment Share on other sites More sharing options...
seventheyejosh Posted April 21, 2010 Share Posted April 21, 2010 I'm confused by your question. If you're updating chat, why would it not be constantly sending requests? setTimeout(x();,100); is only a 100 millisecond interval. 1000 is one second. So of course it's gonna look ridiculous in the tracking terminal. Quote Link to comment Share on other sites More sharing options...
bPHP Posted April 21, 2010 Author Share Posted April 21, 2010 I know... I have it set to 100 because I want it to be faster than one second... In the tracking terminal one second still looks ridiculous. Yes it should send requests, but I've seen other chats and my firebug does not show constant requests. I'm trying to improve my performance as much as possible, I'm constantly sending requests, and each request makes a SELECT from the db... It works fine right now but I'm afraid of what is going to happen if I have 200-300 users all chatting at the same time with this design. Quote Link to comment Share on other sites More sharing options...
seventheyejosh Posted April 21, 2010 Share Posted April 21, 2010 How many active users do you have chatting? Unless you have a writing icon or w/e to notify the other user that their partner is writing, you can probably just use 1 - 1.5 (1000 - 1500) intervals. 100 is a shit load 86,400 seconds a day.. * 10 per second... 864,000 * 2 = 1,728,000 requests to your server.. and that is if there is only one user chatting with one partner... I can't imagine that is good for a server that is unprepared. Quote Link to comment Share on other sites More sharing options...
bPHP Posted April 21, 2010 Author Share Posted April 21, 2010 I don't have any yet because my website is still in development. I've tried with 4 users and worked fine, but yes, it is a big load for the server. And yes, I have a notification that the other partner is writing. I just can't figure out how to do this in a neat way. Any ideas? Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted April 21, 2010 Share Posted April 21, 2010 I would use Java. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.