Introduction to Server-Sent Events

Source: Internet
Author: User

Server-Sent events indicate that the webpage automatically obtains updates from the Server. This was also possible in the past, provided that the webpage had to ask if there were any available updates. When an event is sent through the server, the update will automatically arrive. Example: Facebook/Twitter updates, valuation updates, new blog posts, and competition results. The browser supports all mainstream browsers to send events on the server, except Internet Explorer. The EventSource object that receives Server-Sent Event Notifications is used to receive event notifications from the Server: [html] var source = new EventSource ("demo_sse.php"); source. onmessage = function (event) {document. getElementById ("result "). innerHTML + = event. data + "<br>" ;}; instance resolution: Create a New EventSource object and specify the URL of the page to be updated ("demo_sse.php" in this example ") each time an update is received, an onmessage event occurs. When an onmessage event occurs, push the received data into the element with the id as "result" to check that the Server-Sent event supports the following instances, we wrote an additional code to check the browser support for sending events on the server: [ht Ml] if (typeof (EventSource )! = "Undefined") {// Yes! Server-sent events support! // Some code...} else {// Sorry! No server-sent events support...} to make the above example run, you also need servers (such as PHP and ASP) that can send data updates ). The syntax of the server-side event stream is very simple. Set the "Content-Type" header to "text/event-stream ". Now you can start sending event streams. [Html] <? Php header ('content-Type: text/event-stream'); header ('cache-Control: no-cache'); $ time = date ('R '); echo "data: The server time is: {$ time} nn"; flush ();?> ASP code (VB) (demo_sse.asp): [html] <% 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", which specifies that the page will not be cached and output on the sending date (always with "data: in the above example, we use the onmessage event to get the message. However, you can also use other events: Event Description onopen when the connection to the server is opened onmessage when the message onerror is received when an error occurs

Related Article

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.