HTML5 's server-sent Events (SSE)

Source: Internet
Author: User

HTML5 has a server-sent Events (SSE) feature that allows the server to push data to the client. (usually called data push). Let's take a look at the simple timing diagram of traditional Web application communication:

Now, most of the Web app has Ajax, and that's what it looks like:

Based on data push, when the data source has new data, it is sent to the client immediately, without waiting for the client to request. These new data may be the most news, the latest stock quotes, chat information from friends, weather forecasts and so on.

Data pull and push function is the same, the user to get new data. But there are some advantages to data push. You may have heard of comet, Ajax push, reverse Ajax, HTTP streaming, WebSockets and SSE are different technologies. Perhaps the biggest advantage is low latency. SSE is used for Web applications to refresh data and does not require any action by the user.
You may have heard of HTML5 's websockets, which can also push data to the client. WebSockets is a more complex technology to implement the server, but it is a true full-duplex socket, the server can push the data to the client, the client can also push the data back to the server. SSE works in existence HTTP/HTTPS protocol, support proxy server and authentication technology. SSE is a text protocol you can easily debug it. If you need to send large binary data from the service side to the client, WebSocket is a better choice.

Let's take a look at a very simple example, first the front-end basic_sse.html:

<! DOCTYPE html>

The backend is first a basic_sse.php page:

<?phpheader ("Content-type:text/event-stream"); while (true) {    echo "data:". Date ("Y-m-d h:i:s"). " \ n ";    @ob_flush (); @flush ();    Sleep (1);}? >

HTML5 's server-sent Events (SSE)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.