HTML5 Server-Sent Events

Server-Sent event - one-way messaging

The Server-Sent event refers to the web page automatically getting updates from the server.

It was also possible to do this before, if the webpage had to ask if an update was available. By sending events through the server, updates can arrive automatically.

Examples: Facebook/Twitter updates, valuation updates, new blog posts, event results, etc.

The following is a sample code on W3School, including client-side JavaScript and server-side PHP code:

var source = new EventSource("demo_sse.php");

source.onmessage = function(event) {

document.getElementById("result").innerHTML += event.data + "< br />";

};

This code continuously obtains data from demo_sse.php and outputs the result to the ID of in the div of result.

<?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();

?>

This code returns the current time of the server to the client. Finally, a result similar to the following is displayed on the client page:

The server time is: Fri, 29 Aug 2016 02:03:21 +0800

The server time is: Fri, 29 Aug 2016 02:03:24 +0800

The server time is: Fri, 29 Aug 2016 02:03:27 +0800

The server time is: Fri, 29 Aug 2016 02: 03:30 +0800

The server time is: Fri, 29 Aug 2016 02:03:33 +0800

...

Receive Server-Sent event notification

EventSource object is used to receive server-sent event notification:

Instance

var source=new EventSource("demo_sse.php");source.onmessage=function(event)
{
document.getElementById("result").innerHTML+=event.data + "<br> ";
};


Example analysis:

Create a new EventSource object, and then specify the URL of the page to send updates (in this case, "demo_sse.php")

Every time an update is received, the onmessage event will occur

When the onmessage event occurs, push the received data into the element with the id "result"

Detect Server-Sent event support

In the following example, we wrote an additional piece of code to detect browser support for server-sent events:

if(typeof(EventSource)!=="undefined")
{
// The browser supports Server-Sent
// Some codes...
}
else
{
// The browser does not support Server-Sent..
}

Server-side code example

In order for the above example to work, you will also need a server that can send data updates (such as PHP and ASP).

The syntax of server-side event streaming is very simple. Set the "Content-Type" header to "text/event-stream". Now you can start sending the event stream.

Example

<?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();
?>

ASP code (VB) (demo_sse.asp):

<%
Response.ContentType="text/event-stream"
Response.Expires=-1
Response.Write("data: " & now())
Response.Flush()
%>

Code explanation:

Set the header "Content-Type" to "text/event-stream"

Specify that the page is not cached

Output the sending date (always starting with "data: ")

Refresh output data to the web page




Continuing Learning
||
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>服务器推送SSE</title> <script type="text/javascript"> $(document).ready(function(){ //检查浏览器支持情况 if(typeof(EventSource)!=="undefined") { //定义个对象,用于初始化事件源,这里用c.php这个页面实现 var eSource = new EventSource("c.php"); //detect message receipt eSource.onmessage = function(event) { //将收到的数据展示到页面的ID=content元素中 document.getElementById("content").innerHTML += event.data+'<br />'; }; }else { document.getElementById("content").innerHTML="没有收到服务端Server-Sent数据."; } }); </script> </head> <body> <div id="content"></div> </body> <p>服务器端c.php 页面写在HTML外部</p> </html> <?php // 要声明头部 header("Content-Type: text/event-stream"); header("Cache-Control: no-cache"); // 直接打印当前时间 echo "data: ".date('Y-m-d H:i:s').PHP_EOL; flush(); ?>
submitReset Code