2

all. I have a site with a small chatroom, and my users are increasingly eating my bandwidth, and after having searched for some miracle, I finally stumped onto something sexy:) called, Long Polling from what I understand,

it is a simple method that is supposed to keep the connection betwe en server/client UN-interapted for a longer period of time, and by doing so reduces the need for continues requests by 90%.

Now, I could be wrong. but, how is this "Simple" thing created. Below, you can see my chat script, I created from a tutorial using Jquery. It works fine, but it is without longpolling capabilities.

Here is the PHP and Javascript code (I can't paste them here, as they are too long).

Now, the question is how do I inject the long poll script into my already built codes?

Jeromy French
  • 11,372
  • 13
  • 67
  • 119

1 Answers1

1

I haven't read your code, but here is a tiny example:

function waitForNotification() {
    $.ajax(url, {
        timeout: 60000,
        success: function(e) {
            //do want you want with e
            //and call function again:
            waitForNotification();
        }
    });
}

On the server side, you need to collect all ajax (on url) requests, and release the response only when a new message has been posted. Obviously, the server side method is harder to implement and totally depends on your server architecture (you can use a COMET implementation).

If you implement it by yourself, be careful, in fact a new message m may be posted while the client is processing the success event (and misses the m notification).

Configure your ajax request as you wish (handle other events).

Interesting:

Community
  • 1
  • 1
fso
  • 134
  • 2
  • 12