I play a little with push notifications and want to refresh the page whenever there is a change in the database.
I have this from http://www.screenr.com/SNH :
<?php $filename = dirname(__FILE__).'/data.php'; $lastmodif = isset($_GET['timestamp']) ? $_GET['timestamp'] : 0; $currentmodif = filemtime($filename); while ($currentmodif <= $lastmodif) { usleep(10000); clearstatcache(); $currentmodif = filemtime($filename); } $response = array(); $response['msg'] = file_get_contents($filename); $response['timestamp'] = $currentmodif; echo json_encode($response); ?>
My data.php is a script to get data from a JSON file:
<script>function itnews_overview() { $.getJSON('/ajax.php?type=itnews_overview', function(data) { $.each(data.data, function(option, type) { $('.bjqs').append('<li><span class="date">'+ type.submitted +'<br />'+ type.time +'</span><h2>' + type.title + '</h2><p>' + type.content + '</p></li>'); }); }); } </script> <script> itnews_overview(); </script> <div id="news"> <ul class="bjqs"></ul> </div>
UPDATE: Code from index.php:
<script type="text/javascript"> var timestamp = null; function waitForMsg() { $.ajax({ type: "GET", url: "getData.php?timestamp=" + timestamp, async: true, cache: false, success: function(data) { var json = eval('(' + data + ')'); if(json['msg'] != "") { $(".news").html(json['msg']); } timestamp = json['timestamp']; setTimeout('waitForMsg()',1000); }, error: function(XMLHttpRequest, textStatus, errorThrown){ setTimeout('waitForMsg()',15000); } }); } $(document).ready(function(){ waitForMsg(); }); </script>
Since this file is not saved when I add something to the database, filemtime will not work - is there another way to check if new rows are added to the table?
UPDATE: Attempting to resolve this issue using SSE. I have two files: index.php and send_sse.php (inspiration from http://www.developerdrive.com/2012/03/pushing-updates-to-the-web-page-with-html5-server-sent- events / )
index.php:
<div id="serverData">Content</div> <script type="text/javascript"> </script>
send_sse.php:
<?php header('Content-Type: text/event-stream'); header('Cache-Control: no-cache'); $url = "content.json"; $str = file_get_contents($url); $data = json_decode($str, TRUE); //generate random number for demonstration //echo the new number echo "data: " . json_encode($data); ob_flush(); ?>
This, however, does not seem to work, which is probably due to the fact that SSE needs plain text data. I just can't figure out how to do this, and then wrap this content in a couple of HTML tags.
UPDATE: Well, now it seems to work with SSE, thanks to VDP. I have the following:
$sql= "SELECT title, content, submitted FROM `flex_itnews` where valid = 1 order by submitted desc"; $query= mysql_query($sql); setlocale(LC_ALL, 'da_DK'); while($result = mysql_fetch_array($query)){ echo "data: <li><span class='date'>". strftime('%e. %B', strtotime($result['submitted'])) ."<br />kl. ". strftime('%H.%M', strtotime($result['submitted'])) ."</span><h2>" . $result['title']. "</h2><p>" . $result['content'] ."</p></li>\n"; }
However, when I add something new, it just echoes data: data: data . If I refresh the page, it displays correctly.
UPDATE: Using the livequery plugin:
<script> var source = new EventSource('data2.php'); source.onmessage = function (event) { $('.bjqs').html(event.data); }; $('#news').livequery(function(){ $(this).bjqs({ 'animation' : 'slide', 'showMarkers' : false, 'showControls' : false, 'rotationSpeed': 100, 'width' : 1800, 'height' : 160 }); }); </script>
UPDATE: Trying to use delegate ()
<script> $("body").delegate(".news", "click", function(){ $("#news").bjqs({ 'animation' : 'slide', 'showMarkers' : false, 'showControls' : false, 'rotationSpeed': 100, 'width' : 1800, 'height' : 160 }); var source = new EventSource('data2.php'); source.onmessage = function (event) { $('.bjqs').append(event.data); }; }); </script>