How to create a live blogging platform in PHP which handles 1000 of users at a time?

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.

Answers


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.


If I were you, I will implement some cache logic between the database and the webapp. Otherwise you will have some memory troubles when too much users will be logged in and will query at the same time a lot of blogs posts.

Look this post of stackoverflow and the Official documentation of CakePHP, v2.0, Cache

It is done inside the file app/Config/bootstrap.php of CakePHP:

<?php
Cache::config('short', array(
    'engine' => 'File',
    'duration' => '+1 hours',
    'path' => CACHE,
    'prefix' => 'cake_short_'
));

// long
Cache::config('long', array(
    'engine' => 'File',
    'duration' => '+1 week',
    'probability' => 100,
    'path' => CACHE . 'long' . DS,
));

You have several options listed : FileCache, ApcCache, Wincache, XcacheEngine, MemcacheEngine, RedisEngine. And of course any cache library you could have experimented because cakePHP is enough flexible to integrate it.


Need Your Help

what is the point to have padding 2px top and bottom

html css

In declaration "#message a" I have a style padding that looks like this

LINq query throws exception

c# linq llblgenpro

I am running the following query

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.