Hi all, I try to get notified from DB using pg_get_notify(). However to get it working, I need to stay in same connection session. In order to avoid to put my function in my view, I use an ajax call which run this function like :
Yii::app()->clientScript->registerScript('script',"
var token='".Yii::app()->request->csrfToken."';
setInterval(function() {do_I();}, 5000);
function do_I() {
$.ajax({
type: 'POST',
url: 'getNotify',
data: {YII_CSRF_TOKEN:token},
dataType: 'JSON',
timeout: 6000,
success: function(response){
if(response.success=='notify') {
$.fn.yiiGridView.update('my-grid',{data:{pageSize:$pageSize}});
}
}
});
}",CClientScript::POS_READY);
function actionGetNotify()
{
$conn = Yii::app()->db;
$listen = $conn->createCommand('LISTEN chann;')->query();
//while(1) {
$notify = pg_get_notify($conn);
if ($notify) {
echo json_encode(array('success' => 'notify'));
break;
}
else {
echo json_encode(array('success' => 'nothing'));
}
//}
//exit();
}
However with that, it will open a new connection session every time which cannot work. I tried to use a while(true) until to get a notification, but my browser page stay in loading state indefinitely. So my question is how can I stay in same connection session without staying in same function ?