HTML5 Server-Sent Events
Server-Sent Events - One Way Messaging
A server-sent occasion is the point at which a web page naturally gets refreshes from a server.
This was likewise conceivable some time recently, however the web page would need to inquire as to whether any updates were accessible. With server-sent occasions, the updates come consequently.
Cases: Facebook/Twitter refreshes, stock value refreshes, news sustains, don comes about, and so forth.
Browser Support
Receive Server-Sent Event Notifications
Example
var source = new EventSource("demo_sse.php");
source.onmessage = function(event) {
document.getElementById("result").innerHTML += event.data + "<br>";
};
source.onmessage = function(event) {
document.getElementById("result").innerHTML += event.data + "<br>";
};
Check Server-Sent Events Support
In the tryit example above there were some extra lines of code to check browser support for server-sent events:
if(typeof(EventSource) !== "undefined") {
// Yes! Server-sent events support!
// Some code.....
}
else {
// Sorry! No server-sent events support..
}
Server-Side Code Example
For the example above to work, you need a server capable of sending data updates (like PHP or ASP).The server-side event stream syntax is simple. Set the "Content-Type" header to "text/event-stream". Now you can start sending event streams.
Code in PHP (demo_sse.php):
<?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();
?>
Code in ASP (VB) (demo_sse.asp):
<%
Response.ContentType = "text/event-stream"
Response.Expires = -1
Response.Write("data: The server time is: " & now())
Response.Flush()
%>
Wright © bestwebdesign and Graphics design
0 comments:
Post a Comment