3

I recently asked a question on StackOverflow about a function of mine and people recommended that I use Ajax Long Polling. I have spent the past couple of days researching the subject and have tried to write basic long polling code, but none of it has worked and I can not get anything I do to work at all.

Here is my basic function:

    <script language='javascript' type='text/javascript'>
    var interval=self.setInterval("checkformessages()",4000)
    function checkformessages() {
    $('#incomingmessages<?php echo $otherchatuser ?>').load('checkfornewmessages.php?username=<?php echo $username; ?>&otherchatuser=<?php echo $otherchatuser; ?>');
    }
    </script>

Would someone be able to tell me how to turn this into a basic long polling function, or even just direct on the path that I need to get there. Any help is very much appreciated. Thanks!

Eggo
  • 541
  • 7
  • 18

1 Answers1

6

Usually (i.e. when not using long polling), your JavaScript code will make a request to your server and your server will return with information immediately. However, your server may not always have something important to say immediately. In your example (which seems to be a chat), the person you're chatting with may not have said anything when you make a request to checkfornewmessages.php. Therefore when your JavaScript client asks the server what has been said, the server really has nothing to respond with except "Nothing has been said."

With long polling instead of having checkfornewmessages.php return immediately with "Nothing has been said," you simply don't return from checkfornewmessages.php until there is something important to return with.

In other words, for long polling to work, the interesting stuff is done on the server side probably in your checkfornewmessages.php page. Your javascript code doesn't have to do anything except contact checkfornewmessages.php and wait for it to respond.

Jack Edmonds
  • 31,931
  • 18
  • 65
  • 77
  • So I would have to write a different function that calls chekfornewmessages.php. And then, like my function above, I could load any new changes into a div. – Eggo Feb 01 '12 at 21:31
  • 2
    @Eggo Basically, your call to `load` would contact `checkfornewmessages.php` and then stop and wait for `checkfornewmessages.php` to return with a new message. `checkfornewmessages.php` wouldn't return until someone has said something. This might be a long time, hence the term *long* polling. When someone does say something, the server returns immediately. – Jack Edmonds Feb 01 '12 at 21:33
  • So I think that I understand the basic concept of long polling, but how do I write a long polling function? Nothing that I have tried has seemed to work? – Eggo Feb 01 '12 at 21:48
  • @Eggo One way to do this is to create a `ChatRoom` class and put it in all the members' Sessions. The ChatRoom would have some kind of blocking queue. `checkfornewmessages.php` would try to pull a message off the queue which would block the request from returning until there is a message available. Then another page `sendmessage.php` or something would put something on the queue which would immediately unblock all the `checkfornewmessages.php` requests. – Jack Edmonds Feb 01 '12 at 21:54
  • @Eggo I don't know of any blocking queue implementations for PHP and a quick search didn't reveal any. But you might consider something like Apache ActiveMQ (although that seems like overkill) – Jack Edmonds Feb 01 '12 at 21:56
  • That does look a bit like overkill. I really am just now more confused than ever with this long polling stuff because no one has a simple function online, they are all just overly complicated and even in written detail, they do not make sense to me. I might as well just stick with my original function because I am making no progress very quickly. – Eggo Feb 01 '12 at 21:59