0

I implemented the very easy and lazy method of requesting data from server via ajax in interval for real time affect.

But I want to opt for long polling/ comet technique as they encourage server to push data when there's one instead of client pulling constantly even when there's none.

I completely understand the way long polling and comet work. But I wish to see the coding part where server pushes when there's data to client.

How can for a request made by client long ago (long polling) can server push when there's data? I don't understand this part. Can someone show in coding please?

currently i check via ajax for new data in interval. Now, how can this be modified to implement long polling where server returns automatically when there's data without client asking for it?

(function notify(){
    $.each(id, function(k, v){
      jQuery.ajax({

           method: "POST",
           url: "/notification.php",
           "data": v,
           error: function() {
             reject('error');
           },  
           success: function(result) {
             console.log(result.data);         
           }
       });
    })
setTimeout(function(){
    notify();
 }, 1000);
})()

Since this question marked as duplicate:

I said I fully understand the concept of long polling and comet. I'm here not to understand the concept but for the code that actually implement that concept!

112233
  • 2,406
  • 3
  • 38
  • 88
  • Possible duplicate of [What are Long-Polling, Websockets, Server-Sent Events (SSE) and Comet?](http://stackoverflow.com/questions/11077857/what-are-long-polling-websockets-server-sent-events-sse-and-comet) – Alexander O'Mara Aug 20 '16 at 06:56
  • Basically the server just doesn't respond until data becomes available. BTW, Regular old PHP really isn't designed for this, as it is not really scale-able and you will quickly use up all resources. – Alexander O'Mara Aug 20 '16 at 07:00
  • @AlexanderO'Mara, i'll make sure my server uses php5 and above. But how does the server respond, can you show the pseudo code? – 112233 Aug 20 '16 at 07:03
  • It responds the same as normal, just after waiting for something to happen. Also it doesn't matter if your using PHP 5 or 7, in a typical setup every active requests needs a PHP process to run. Long polling means that every request has a PHP process almost constantly idling, which will quickly eat up resources. – Alexander O'Mara Aug 20 '16 at 07:12
  • @AlexanderO'Mara, the conclusion is? – 112233 Aug 20 '16 at 07:19
  • Don't use traditional PHP for such things. – Alexander O'Mara Aug 20 '16 at 07:20
  • Don't use php5 and above for such things. You need either a non-blocking framework (reactphp, amphp etc.) or something with very lightweight threads like java, C# or C++. One of the reasons node.js is so popular is because it is by default non-blocking so can be used for long polling projects without any additional change in how you write code. – slebetman Aug 20 '16 at 07:27
  • @slebetman, I'mm quite interested in nodejs too..but one problm I consider is, nodeJs has to be installed. I'm afraid my non-tech client would find it difficult to install it on their server...some are even using shared hosting..Is there a way to overcome this? – 112233 Aug 20 '16 at 07:37

1 Answers1

0

Finally I found the answer here:

http://www.zeitoun.net/articles/comet_and_php/start

I opted for the second example given in the site : comet with classic ajax It works like charm. Below is the code for index.html (client page) in jquery also taken from the site..

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Comet demo</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!--     <script type="text/javascript" src="prototype.js"></script> -->
    <script type="text/javascript" src="jquery.js"></script>
  </head>
 <body>
<div id="content"></div>
<div style="margin: 5px 0;">
<form id="cometForm" action="javascript:void(0);" method="get">
<input id="word" type="text" name="word" value=""/>
<input type="submit" name="submit" value="Send"/>
</form>
</div>


<script type="text/javascript">
(function($){
function handleResponse(response){
$('#content').append('<div>' + response['msg'] + '</div>');
}

var timestamp = 0;
var url = './chat_backend.php';
var noerror = true;
var ajax;

function connect() {
ajax = $.ajax(url, {
type: 'get',
data: { 'timestamp' : timestamp },
success: function(transport) {
eval('var response = '+transport);
timestamp = response['timestamp'];
handleResponse(response);
noerror = true;
},
complete: function(transport) {
(!noerror) && setTimeout(function(){ connect() }, 5000) || connect();
noerror = false;
}
});
}

function doRequest(request) {
$.ajax(url, {
type: 'get',
data: { 'msg' : request }
});
}

$('#cometForm').on('submit', function(){
doRequest($('#word').val());
$('#word').val('');
return false;
});

$(document).ready(function(){
connect();
});
})(jQuery);
</script>

</body>
</html>
112233
  • 2,406
  • 3
  • 38
  • 88