I am working on live blogging platform which must be implemented using CakePhp or Php.
It must handle 1000 of users at a time.
I’ve done this by auto refreshing the content using ajax when a new data is inserted in mysql table. The problem is that it hits the DB every 10 seconds to check whether the data is inserted in the past 60 seconds. If the count results 1 then I programmed to refresh the content.But it doesn’t feel like an optimised solution.
Is there any way to implement this?
My code – ajax call to livefeed controller :
var auto_refresh = setInterval(
function()
{
$.post('/blog/posts/liveFeed', function(data) {
if(data >0){
alert(data);
$('#content').load(window.location.href);
}
});
return false;
}, 10000);
Livefeed controller in posts.php:
public function liveFeed() {
$res=$this->Post->query('SELECT count(*) AS data FROM posts where TIME_TO_SEC(TIMEDIFF(NOW(), created)) < 60');
echo $res[0][0]['data'];
}
I’m not good at cakePhp. So forgive me If my code looks unprofessional.
You could use some caching.
Use an http-cache like varnish and reset it every time a new article gets written – Otherwise just deliver the html page from varnish. That should give you a considerable boost in speed and possible users.
You can still reload the page every 10 seconds with an ajax request – It would just load the cached page and not query the database every time.