1

This is my ajax code so please can anyone tell me how can i change this code to long polling ?

Here is my code :-

var chat = {}
chat.fetchMessages = function () {
  $.ajax({
    url: 'ajax/ajax/chat.php',
    type: 'POST',
    data: { method: 'fetch' },
    success: function(data) {
      $('#chats').html(data);
    }
  }); 
}
chat.interval = setInterval(chat.fetchMessages, 1000);
Mmmh mmh
  • 5,334
  • 3
  • 21
  • 29
Yassin
  • 1,049
  • 11
  • 15
  • [**What have you tried?**](http://whathaveyoutried.com) Also, is this the same as your question you asked 1 hr ago - http://stackoverflow.com/questions/15838715/how-to-change-from-ajax-polling-to-long-polling ? If so, you should revise your original question, rather than just asking again - [Can I re-ask a question if it hasn't been answered?](http://meta.stackexchange.com/questions/473/can-i-re-ask-a-question-if-it-hasnt-been-answered) [How do I get attention for old, unanswered questions?](http://meta.stackexchange.com/questions/7046/how-do-i-get-attention-for-old-unanswered-questions) – Sean Apr 05 '13 at 18:00

2 Answers2

2

You have to put the next call of fetchMessage in the callback of the previous one :

var chat = {}
chat.fetchMessages = function () {
  $.ajax({
    url: 'ajax/ajax/chat.php',
    type: 'POST',
    data: { method: 'fetch' },
    success: function(data) {
      $('#chats').html(data);
      chat.fetchMessages(); // let's do it again
    }
  }); 
}
chat.fetchMessages(); // first call
Mmmh mmh
  • 5,334
  • 3
  • 21
  • 29
  • 1
    Just as a note, you probably want to put some kind of time delay on the call (i.e. wrap the recursive `chat.fetchMessages();` in a `setTimeout` function. Otherwise, you run the risk of slowing down and/or crashing your entire server environment due to an overload of opening and closing connections from the ajax requests. – War10ck Apr 05 '13 at 17:59
  • @War10ck Thanks for your help but i did what you said and nothing new happened it even got faster to send requests that is my new code :- var chat = {} chat.fetchMessages = function () { $.ajax({ url: 'ajax/ajax/chat.php', type: 'POST', data: { method: 'fetch' }, success: function(data) { $('#chats').html(data); $('#chatshere').scrollTop($('#chatshere')[0].scrollHeight); setTimeout(chat.fetchMessages(),30000); } }); } chat.fetchMessages() – Yassin Apr 05 '13 at 18:12
  • @Magikano Try removing the `()` in the `setTimeout` statement. It should read `setTimeout(chat.fetchMessages, 30000);`. As it is currently written the function will be evaluated immediately as the open and close parenthesis `()` represent a function call. In this case, the function should not be evaluated immediately but as a _callback_ of the `setTimeout` function. Check out this simple [fiddle](http://jsfiddle.net/HsNyy/) that illustrates this point. – War10ck Apr 05 '13 at 18:22
  • @War10ck Thanks for your replay i did what you said but it also keeps getting request every second chat.php then after 1 second chat.php ant thats what i dont want , i want it to wait until something new comes up in the database then it gets a request , that what i thought long polling will do for me ! – Yassin Apr 05 '13 at 18:29
  • @Magikano Okay, that's good then. My apologies. I must have misunderstood what you were asking for. That's good that it is working though. :) Good luck and happy coding! – War10ck Apr 05 '13 at 18:32
  • @War10ck thanks but now i have this code in short polling so how i can make it long polling ! – Yassin Apr 05 '13 at 18:37
  • 1
    @Magikano Let me see if I'm following you. You want to only make the ajax request when new chat data is available? It sounds like what you want is a _push_ service. A _push_ service is where the client stays idle. When the server gets new information it automatically pushes it to the client who receives it and takes the appropriate action. Unfortunately I have only read of such technologies. I haven't ever actually coded them. My apologies buddy but that is pretty much the extent of my knowledge on push technology. – War10ck Apr 05 '13 at 18:59
  • @War10ck ok no problem thanks very much for all your replies and i will try to google "push service" and know more about it . thanks alot :) – Yassin Apr 05 '13 at 19:03
  • @Magikano not a problem buddy. Hope it works out for you. Good luck and happy coding! :) – War10ck Apr 05 '13 at 19:03
0

The above code seems to work fine but this will call function immediately as a result of this it would increase the traffic and possibly for continuous long polling memory usage by the browser will increase . Try to use settimeout to keep some duration between calls made , would be good if you clear cache too . Other option would be comet or signaR .

user1260967
  • 89
  • 1
  • 2
  • 14
  • ok i don't want it to keep requesting every second i want it to just bring all the new messages when it is available , and i tried to learn the comet but i found it very confusing and difficult to use so i want to make it with long polling because i think it is easier ! – Yassin Apr 06 '13 at 12:36
  • as i said it may increase your browser memory usage continously resulting in browser crash . Any other alternative could be sheepjax . – user1260967 Apr 06 '13 at 12:44
  • yeah because this is short polling but if it was long polling it would be better , right ? – Yassin Apr 06 '13 at 12:46