1

I have a page where i have some text that I take from the database. While that page is open and someone else changes the text in the database, I wan't to make it so that my page is immediatly refreshed when the data changes.

I have googled around and found some stuff about using comet, but I don't understand anything about it.

Is there a more simple way to solve my problem? and if not, does anyone know a simple tutorial for comet?

Dumisani
  • 2,988
  • 1
  • 29
  • 40
user3322672
  • 153
  • 2
  • 13

1 Answers1

1

There are two methods that come to my mind. The first in the category 'push-messages'. Not a lot of experience with that, so im going to continue to option 2:

Add a last-edit timestamp in the same table/row as text is. When someone updates the text, update the timestamp.

When an user loads a page, also send the last-edit timestamp in a javascript variable. Then make a super-lightweight AJAXcall to a verify-file, along the lines of this:

$query = "SELECT text,last_edit FROM someTable WHERE lastedit>".$_GET['timestampFromJS']." LIMIT 1";
$result = mysqli_query($conn, $query);
if( $result->num_rows===0 ){
    echo 0; // something small for JS
}
else{
    $fetch = $result->fetch_assoc();
    echo $fetch['text'];
}

In the success handler of your javascript code, check if the value is not equal to 0, if it's not 0, use the result to update the page.

There might be other solutions, or you might prefer JSON (I know I do :P), but you'll get the draft of the idea :)

Martijn
  • 15,791
  • 4
  • 36
  • 68