Question

I'm trying to make an HTML5-based notification system, based on when something occurs on a table in my database. I have specifically made the table for this notification feature . This is how it works:

  1. Server-Sent Events page (sse.html) will refer to source.php for its notification content.
  2. action.php contains the action that would trigger a change of content notification by inserting data in to a table.
  3. source.php will periodically check for any new data in table. It should pass a notification if there's any.

My question is it possible if to do this without checking the table?

I'm thinking of using cURL to directly send content to source.php when the action in action.php is performed. Can this be done?

I've seen cURL used with the HTTP header Content-Type set to text/event-stream, but I'm not sure how to use it.

sse.html

<!DOCTYPE html>  
<html>  
<body>  
  <script>  
    var source = new EventSource('source.php');  
    source.onmessage = function(event){  
      var text = event.data;  
      window.webkitNotifications.createNotification('', 'Alert', text).show();  
    }  
  </script>  
</body>  
</html>

source.php

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

mysql_connect("localhost", "user", "pass");
mysql_select_db("eventstream");

$q = mysql_query("select textnotif from notification where read='0'");
$r = mysql_fetch_array($q);

$notif = $r[textnotif];

if($notif != ""){  
    echo "data: ".$notif.PHP_EOL;  
}
Was it helpful?

Solution

To answer your question:

... is it possible if to do this without checking the table?

No.

Your PHP script has to check the SQL table to see if any data has changed. The only way PHP can check for a database change is to query the database.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top