Server Sent Events
It takes the updates from server and gives result on web
browsers.Before take updates from server,browser would have to ask, if
any updates were available in web servers.
Example
HTML5 code should be as follows
<html>
<body>
<h1> Server updates</h1>
<div id="result"></div>
<script>
if(typeof(EventSource) !== "undefined") {
var source = new EventSource("demo_sse.php");
source.onmessage = function(event) {
document.getElementById("result").innerHTML += event.data + "<br>";
};
}
else {
document.getElementById("result").innerHTML = "Sorry, your browser does not support ";
}
</script>
</body>
</html>
demo_sse.php
Server Code should be in PHP,It looks like as follows
<?php
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
$time = date('r');
echo "data: The server time is: {$time}\n\n";
flush();
?>
The server time is: Thu, 23 Feb 2017 20:20:22 +0530
The server time is: Thu, 23 Feb 2017 20:20:30 +0530
The server time is: Thu, 23 Feb 2017 20:20:37 +0530
The server time is: Thu, 23 Feb 2017 20:20:44 +0530
The server time is: Thu, 23 Feb 2017 20:20:50 +0530
The server time is: Thu, 23 Feb 2017 20:20:56 +0530
The server time is: Thu, 23 Feb 2017 20:21:02 +0530
The server time is: Thu, 23 Feb 2017 20:21:10 +0530
The server time is: Thu, 23 Feb 2017 20:21:16 +0530
The server time is: Thu, 23 Feb 2017 20:21:22 +0530
The server time is: Thu, 23 Feb 2017 20:21:28 +0530
The server time is: Thu, 23 Feb 2017 20:21:34 +0530
The server time is: Thu, 23 Feb 2017 20:21:40 +0530
The server time is: Thu, 23 Feb 2017 20:21:45 +0530
The server time is: Thu, 23 Feb 2017 20:21:52 +0530
The server time is: Thu, 23 Feb 2017 20:21:57 +0530
The server time is: Thu, 23 Feb 2017 20:22:03 +0530
The server time is: Thu, 23 Feb 2017 20:22:09 +0530
The server time is: Thu, 23 Feb 2017 20:22:15 +0530
The server time is: Thu, 23 Feb 2017 20:22:20 +0530
The server time is: Thu, 23 Feb 2017 20:22:26 +0530
You can get demo of Server Sent Events
here
No comments:
Post a Comment