php_nub_qq Posted May 7, 2013 Share Posted May 7, 2013 Hello freaks. I'm in need of assistance because I just figured out my whole system might turn out to be a big load of yeah.. I have created a chat system which uses polling to update stuff, except that instead of returning a "no new messages" response it loops through the script again. It looks something like this while(true){ Check for messages; if(messages) break; } What the problem is ( that I'm aware of ) is that I can't stop the script unless it returns new messages ( executes on it's own until the end ). Even if I remotely call the xHttp request's abort() method it only gets aborted on the client side, but the script keeps running. After looking for solutions I came up with this ignore_user_abort(false); while(!connection_aborted(){ } which was supposed to exit the loop when the client closes their browser, but it doesn't. The only thing I can do is have max execution time stop the script, but this script is supposed to run for like at least 15 minutes before timing out, and if the client closes their browser on the 5th second there goes 14 minutes of waste I'm in a desperate need of help, please! Quote Link to comment Share on other sites More sharing options...
Irate Posted May 7, 2013 Share Posted May 7, 2013 How about looking for messages and breaking while there are no messages? A while(true) loop is a hefty workload... Quote Link to comment Share on other sites More sharing options...
php_nub_qq Posted May 7, 2013 Author Share Posted May 7, 2013 (edited) That is what I am trying to avoid, sending empty requests. I think I'm on the path to solve my issue. Apparently php's connection handling gets updated only when data is outputted from the server so now I'm flushing data out but it is still not working. I'm now working on a custom xhr onreadystatechange function to receive data in pieces, to see if data is actually sent, since I'm using jquery and it's readystate handler cannot be modified from what I've read. Going to keep you updated on my progress Edited May 7, 2013 by php_nub_qq Quote Link to comment Share on other sites More sharing options...
php_nub_qq Posted May 7, 2013 Author Share Posted May 7, 2013 (edited) I just found out what the problem is: if there is any requests to a database in the loop PHP doesnt push any data with flush() and ob_flush() even if it is a simple echo, does anyone have a solution to that? Edited May 7, 2013 by php_nub_qq Quote Link to comment Share on other sites More sharing options...
php_nub_qq Posted May 7, 2013 Author Share Posted May 7, 2013 UPDATE: It seems there is no easy way around this. I've just set the maximum execution time to 10 seconds so that scrips don;t get left hanging there. I will keep track of this thread anyway if anyone replies! Quote Link to comment Share on other sites More sharing options...
kicken Posted May 7, 2013 Share Posted May 7, 2013 A database request shouldn't affect how flush/ob_flush work. If they are not working properly then most likely there is something else down the line buffering your data which you have no control over (ie, the webserver, proxy, cache server, etc). Also I don't know what your full code is, but if you are going to do a while(true) loop like that then you should include a delay in each iteration. That way even if your script does end up running for a long time it is not chewing up all the CPU cycles and DB processing checking for nothing. Something like: while (!$messages = GetNewMessages()){ sleep(1); //Pause for a second } If you really want to do long-polling then a more ideal solution would be to have a separate server which the clients connect to for message checking which handles the situation better. The main problem with doing this with something like apache is that while your script is sitting there waiting for new messages, it's tying up processing threads which apache uses to handle requests. If you end up tying up all the processing threads with your polling script nobody would be able to browse your site at all. That is one of the reasons that short-polling (ie, returning a 'nothing new' response, despite being a bit of a bandwidth waste, is preferred. Quote Link to comment Share on other sites More sharing options...
php_nub_qq Posted May 7, 2013 Author Share Posted May 7, 2013 (edited) Of course I have a delay. I can confirm that the problem is with the database requests because if I remove them it works, see: doesn't work while(true){ $messages = $msg->getMessages($ajaxData); if(!empty($messages)){ $break = true; } $conversation = $msg->getConversations($ajaxData[2], $ajaxData[4]); if(!empty($conversation)){ $break = true; } $loop++; if($loop > (10/$interval) || $break){ break; } usleep($interval*1000000); echo json_encode(array('response' => 'test')); } works while(true){ //$messages = $msg->getMessages($ajaxData); if(!empty($messages)){ $break = true; } //$conversation = $msg->getConversations($ajaxData[2], $ajaxData[4]); if(!empty($conversation)){ $break = true; } $loop++; if($loop > (10/$interval) || $break){ break; } usleep($interval*1000000); echo json_encode(array('response' => 'test')); } "works" meaning that it pushes data partially. EDIT: all those methods do is verify the data given then connect to the DB, retrieve and return the data Edited May 7, 2013 by php_nub_qq Quote Link to comment Share on other sites More sharing options...
Q695 Posted May 8, 2013 Share Posted May 8, 2013 While what is true include a loop counter beyond the preset limit? You should really do a limit per page on the site with mysql. Quote Link to comment Share on other sites More sharing options...
php_nub_qq Posted May 8, 2013 Author Share Posted May 8, 2013 (edited) While what is true include a loop counter beyond the preset limit? You should really do a limit per page on the site with mysql. while(true){ $messages = $msg->getMessages($ajaxData); if(!empty($messages)){ $break = true; } $conversation = $msg->getConversations($ajaxData[2], $ajaxData[4]); if(!empty($conversation)){ $break = true; } $loop++; if($loop > (10/$interval) || $break){ break; } usleep($interval*1000000); echo json_encode(array('response' => 'test')); } $loop++; if($loop > (10/$interval) || $break){ break; } Edited May 8, 2013 by php_nub_qq 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.