I'm trying to implement long polling in a php chat script, however the long polling puts all the ajax request i send to sleep waiting for the original one.
BTW i'm working with symfony framework.
Any idea ?
-- UPDATE --
Here is some code snippets
Javascript :
function whosTyping(person_id){
$.ajax({
type:'POST',
url:'/chat/whoisTyping',
data:'person_id='+person_id
dataType:'json',
success:function(resp){
if(resp == 'true') $('.is_typing').show();
else $('.is_typing').hide();
setTimeout(function(){
whosTyping(person_id)
},1000)
}
})
}
PHP:
public function executeWhoisTyping(sfWebRequest $request) {
$this->setLayout(false);
$this->setTemplate(false);
sfConfig::set('sf_web_debug', false);
$person_id = $request->getParameter('person_id');
$target_person_id = $this->getUser()->getGuardUser()->getPerson()->getId();
$check = Doctrine_Core::getTable('Typing')->findByPersonIdAndTargetPersonId($person_id)->toArray();
while(empty($check)){
usleep(1000);
clearstatcache();
$check = Doctrine_Core::getTable('Typing')->findByPersonIdAndTargetPersonId($person_id)->toArray();
}
Doctrine_Core::getTable('Typing')->createQuery()
->delete()
->where('target_person_id = ?', $target_person_id)
->execute();
return $this->renderText(json_encode('true'));
}
And yes i'm trying to send regular ajax requests but the they get canceled waiting for the long polling response"
It's ok guys i figured it out
the thing is to make it work with symfony i had to end the current session using session_write_close()
so the action function become the follwing
public function executeWhoisTyping(sfWebRequest $request) {
$this->setLayout(false);
$this->setTemplate(false);
sfConfig::set('sf_web_debug', false);
$person_id = $request->getParameter('person_id');
$target_person_id = $this->getUser()->getGuardUser()->getPerson()->getId();
$check = Doctrine_Core::getTable('Typing')->findByPersonIdAndTargetPersonId($person_id,$target_person_id)->toArray();
while(empty($check)){
usleep(100000);
clearstatcache();
session_write_close();
$check = Doctrine_Core::getTable('Typing')->findByPersonIdAndTargetPersonId($person_id,$target_person_id)->toArray();
}
return $this->renderText(json_encode(!empty($check) ? 'true' : 'false'));
}
hope that helps