Php – Displaying items with a countdown timer

ajaxMySQLPHP

I am creating a widget for rotating topics. The functionality is as follows:

  • Each topic is displayed one by one on the homepage and has a duration of 30 seconds.
  • A countdown timer is displayed on the page. When it reaches zero the current topic gets marked as "shown" and the next topic is displayed.
  • Users can choose to "like" topics, in which case the topic gets an additional 5 seconds duration (the countdown timer needs to update itself at this point).
  • The countdown timer always needs to display the current allocated duration for the topic, so for example if a user "likes" a topic then people accessing the site should instantly see the updated duration for that topic. Basically everything needs to be shown in "realtime".
    The topics are stored in a MySQL database. I am having trouble with the last two points in the above.

The way I have tried to do it so far is to have an AJAX request which is sent every second to deduct 1 second from the current topic duration. The request response is the current topic duration.

The problem with this is that it will only work so long as somebody is accessing the site. I know I can probably create a CRON script to run this process in the background, but for it to run every second would be a bit crazy. There has got to a be a much simpler solution to this problem. What would be the most efficient way to go about this?

Best Answer

If every user will be calling your website every second you're in for trouble. It's not really a scaleable solution and I assume you want this site to be big and famous with thousands of users.

My suggestion is as follows:

  • The topic table should store information on until what time a given topic is the current topic, named expire_time or something.
  • When a user requests a page, send the page together with the number of seconds that are left until the page expires and a new one has to be loaded. Since the time can't be shorter (just longer) there's no idea to make a check every second. You did mention that you wanted remaining time to be "realtime" but I suggest you "fake" that by only making a request every 5-10 seconds or something. You can still have a countdown timer. And since the Like adds 5 seconds there is no point in having greater accuracy than that in your "realtime".
  • When someone presses the like button you just add 5 seconds to the expire_time column.
  • When a user's page times out (or every 5-10 seconds) a request is made to the server with the current viewed topic id as argument. If someone has pressed Like then an Extended timeout response can be given with the extended amount of seconds that the topic is to be shown. You don't need to pass the topic text again because the user already has it. If no one has pressed like, and the topic has expired, then the next topic is returned and shown and the user page starts a new 30 sec countdown.
  • If no user is online then there is no need to be rotating topics so I don't see the need of a cron job. Instead, when the first user comes and there is no active topic, the server just selects a new topic as active and sets a 30 sec expiration on it.

I would also suggest:

  • Set a maximum allowed amount of likes for a certain topic. If 1000 people likes a topic it'll be shown for almost 1,5 hours and for those that want to see the next topic that might be boring. Maybe you can have steps in time added, so the first likes add 5 sec but when you have 10 likes it only add 4 sec, after 50 likes only 3 sec, after 200 likes only 2 sec and so on.
  • You probably also need some kind of IP or cookie registration per topic id so not one user makes a script that spams you with likes and then one topic is shown forever.
  • Realtime is nice but also very heavy for the server when you have many users. My suggestion is at least to have a parameter in your code where you can switch in-between refreshing every 5-10 seconds and to wait all the way down to expiry time before making a new refresh. This way you have a turbo injection for your program in case you get too many users and temporarily need extra speed on the server.

Good luck with your project!

Related Topic