0

I want to create notification system in my company's erp similar to Facebook one. To maintain good performance, I use long polling - looped ajax querying php script for number of seconds. Everything works fine, until I try to go to another page inside ERP. When I click any link on the page, everything freezes waiting until background php script is completed, even if I manually killed ajax connection. JS script is included on every page and starts itself on page load.

function notificationsObject(){
  var nl = new Object();

nl.startAjax = function(data){
    if(nl.ajaxObject != null) try{ nl.ajaxObject.abort() } catch(e){} finally{nl.ajaxObject = null}
    nl.ajaxObject = $.ajax({
      url: nl.ajaxUrl, //declared before function declaration
      type: 'POST',
      data: {data: data}
    }).done(function(responseText){nl.ajaxSuccess(responseText)
    }).fail(function(responseText){nl.ajaxFail(responseText)});

  }

  nl.ajaxSuccess = function(response){
      console.debug(response);
      nl.startAjax();
  }

  nl.ajaxFail = function(response){
    //@todo some code here
  }


  nl.killConnection = function(){
    if(nl.ajaxObject != null) try{ nl.ajaxObject.abort() } catch(e){} finally{nl.ajaxObject = null}
    console.debug('killing');
  }

(more code here)
return nl;
}

init code looks like this

$(document).ready(function(){

  var notifications = notificationsObject();
  notifications.startAjax({name: 'startup'});

  setTimeout(function(){window.onbeforeunload = function(){notifications.killConnection()};}, 1000);

});

and there's also some PHP code:

public function executeUsersNotificationListener(){
    ignore_user_abort(false);
    ob_end_flush();
    $this->getResponse()->setHttpHeader("Cache-Control", "no-cache");
    $this->getResponse()->setHttpHeader("Pragma", "no-cache");
    $this->getResponse()->setHttpHeader("Expires", 0);

    $timeLimit = 30;
    set_time_limit($timeLimit+1);

    echo 'test';

    $i = 0;
    while($i++ < $timeLimit){

      echo "       ";

      sleep(1);
    }

    return sfView::NONE;
  }

as you can see above, I did some research and used ignore_user_abort and so on, but it won't work.

ex3v
  • 3,518
  • 4
  • 33
  • 55
  • I would use [http://en.wikipedia.org/wiki/WebSocket](http://en.wikipedia.org/wiki/WebSocket) for realtime messages. – bitWorking Mar 27 '13 at 12:43
  • Thanks, but I can't find any well documented implementation for webSocket in jQuery. I know that it must work somehow, since so many pages use long polling technique. – ex3v Mar 27 '13 at 12:51
  • There is not websocket support in jquery. But it is really simple to use... – fredrik Mar 27 '13 at 12:53
  • Try [http://socket.io/](http://socket.io/) is has many fallbacks including long polling and flash sockets. – bitWorking Mar 27 '13 at 12:55
  • Thank you all. Websockets looks like interesting technology to use. Unfortunately I can't find any info about starting server with companion of Symfony. Also our admin doesn't want to open additional port. We also can't run another server in another technology, because of our database, which is more than 200 tables, with alredy written mappers. Of cours I will read documentation of websocket, but the main problem for now is - why does Apache holds rendering new page when there's php script running with no active connections. – ex3v Mar 27 '13 at 13:18

0 Answers0