Search code examples
phplong-polling

Do you post old data back when doing a long poll?


I think I understand the concept of polling fairly well. You basically just request data from the server, but only once the data has changed, does the server return it. Straight forward stuff. My problem comes with this example.

Let's say I have auctions with data that changes constantly. Among this data are things like

  • Closing time of the auction
  • Number of current bidders on the auction

When I start the long poll, I basically have something like this:

while($counter < $MESSAGE_TIMEOUT_SECONDS) {
  $newData = getNewData();
  $hasDataChanged = hasDataChanged($newData, $oldData);
  if (  $hasDataChanged  ) {
     return $newData;
  }

  usleep($MESSAGE_POLL_MICROSECONDS);

}

Where do I get the old data from? I mean, when doing the request, I can either post the current state as it was last given to me, or I can store the data in Session. Am I allowed to store stuff in session when doing a long poll, or should I do a POST from the Javascript with the current state of that page?

Also, how would I stop someone opening 50 pages from killing the database? I mean, getNewData() effectively goes to the database. With a polling interval of about half a second, this could mean 50 requests every half a second, which could mean 50 x 2 x 30 = 3000 requests to the database in 30 seconds by just one user, if he decided to open 50 tabs?

Any ideas?


Solution

  • I would cache all ajax response data in memory along with last date that each auction had any change so you don't have to compare old and new data but just datetime. Invalidate cache on some change (closed, new bid, etc.) for auction.

    Then from client side send time from last known data (last ajax call or when user opened page) and compare dates to see if something changed, if it didn't just return status:nochange (now client side knows there is nothing to update) and if it did return all necessary data from cache and update users page.

    This model should protect database from overloading.