I try to use server side event with updating or adding data in database. In this case, why the onmessage event in index.html doesn't work after I add data to database by dataAdd.php?
index.html :
<body>
<div id="result"></div>
</body>
<script>
var result = document.getElementById("result")l
if(typeof(EventSource) !== "undefined") {
var source = new EventSource("sse.php");
source.onmessage = function(event) {
result.innerHTML += event.data + "<br>";
};
} else {
result.innerHTML = "Sorry, your browser does not support sse";
}
</script>
sse.php :
<?php
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
mysql_connect("localhost","username","password");
mysql_select_db("database");
mysql_query("set names utf8");
$result = mysql_query("select * from table order by id desc limit 1;");
$row = mysql_fetch_array($result);
echo "data: $row[message]";
flush();
?>
dataAdd.php :
<?php
mysql_connect("localhost","username","password");
mysql_select_db("database");
mysql_query("set names utf8");
if(isset($_GET['text'])) {
mysql_query("insert into chat (message) values ('$_GET[text]')");
}
?>