0

I am currently trying to create a small chat website for a dedicated group of people.

The majority of the site is complete and working except from one major part which I continue to get completely lost within.

I need to send the data from the server ONLY when a piece of a database in MySQL has updated.

Firstly, I don't know how to have PHP check this database recursively every second or less. I have tried using AJAX but have noticed that this polls the server way too often and uses far too much bandwidth, I have also attempted to use Server-Sent Events with no avail due to it only sending data every 3 seconds which is FAR too slow.

In short; I need a type of push service which sends data TO the client periodically every 1 second or less and will only do this when it is needed.

    while(1)    
{
    $result = mysqli_query($con,"SELECT Username, Message, DateT FROM Chat ORDER BY ID DESC LIMIT 30");

    $list = array();

echo "data: ";
    while($row = mysqli_fetch_array($result))
    {
    echo '<tr><td class="user">'.$row['Username'].'</td><td class="text">'.base64_decode($row['Message']).'</td><td class="time">'.$row['DateT'].' [GMT]</td></tr>';
    }
echo PHP_EOL.PHP_EOL;
flush();
ob_flush();
sleep(1);
}
Stevob21
  • 45
  • 7
  • 4
    Have you looked into websockets? The server can send data to clients without clients requesting it with websockets. You can configure your websocket server to send data to you clients when it receives data that will be written to the database. If you don't want to use websockets, you can use a cronjob to check your DB periodically and then send data when needed, but cronjobs cannot fire more frequently than one time every minute, I believe. – jpodwys Oct 25 '13 at 21:43
  • You can use websockets in PHP. Use [Rachet](http://socketo.me/) – Dev Utkarsh Oct 25 '13 at 21:46
  • 1
    Websockets is the best for PUSH service. BTW try Node.js for building the websocket service. You can thank me later :) – Akshaya Shanbhogue Oct 25 '13 at 21:47
  • Did you bother searching for “php chat”? They’ll be millions or results, most of them people like you asking over and over again, “How do I make a chat system in PHP?” because they didn’t bother to search first. This is not new ground. – Martin Bean Oct 25 '13 at 22:11
  • I've tried searching this many times though I seem to have problems with the 'retry' variable within Server-Sent Events as it is blatantly ignoring anything I set it as. – Stevob21 Oct 25 '13 at 22:15
  • possible duplicate of [Ajax push system](http://stackoverflow.com/questions/7594425/ajax-push-system) – RandomSeed Oct 26 '13 at 13:43

2 Answers2

3

The thing you are looking for is called long-polling and to tell the truth, php is not really a good suit for this. You can look at this question of how to implement long polling in php as well as some other google searches.

Another option can be to use HTML5 websockets to reduce the bandwith. You can read good answers here as well.

There is a really good library which utilize node.js and it is called socket.io

Community
  • 1
  • 1
Salvador Dali
  • 214,103
  • 147
  • 703
  • 753
2

Server side events are a good fit for a push service. They are simpler then websockets because they are read only, they are also easier to support on older browsers with a javascript polyfil rather then having to fallback to a plugin like flash.

A really simple script would look like this:

header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');

while (1) {
    if($updated = check_for_updates()){
         echo "id: " . $updated['id'] . PHP_EOL;
         echo "data: " . $updated['msg'] . PHP_EOL;
         echo PHP_EOL;
         ob_flush();
         flush(); 
    }
    sleep(1);
}

on the client side you would have:

var source = new EventSource('stream.php');

source.addEventListener('message', function(e) {
   console.log(e.data);
}, false);
ejrowley
  • 503
  • 5
  • 12
  • Is there any definite way to get this streaming data far more often? I've tried this method but failed due to it only sending every 3 seconds exactly, I've also tried echoing the 'retry :400' which doesn't effect a thing! Thanks for the quick answers. – Stevob21 Oct 25 '13 at 22:17
  • Your server is closing the connection, you need a long running response (hence the while(1) in the example) – ejrowley Oct 25 '13 at 22:22
  • It must be noted that PHP is not great at long running responses, which is why people recommend node.js – ejrowley Oct 25 '13 at 22:23
  • Just tried this, it does seems to be sending the data only once on page load and then it completely stops! I'll edit the post and include my code! – Stevob21 Oct 25 '13 at 22:43