NEW

6/recent/ticker-posts

Header Ads Widget

HTML5 Server-Sent Event

 

HTML5 Server-Sent Event

The HTML5 server-sent event enables a browser to receive automatic updates and data from a server via HTTP connections.

What are the Server-Sent Events?

Whenever we perform some event and send it to the server such as by submitting the form to the server. So such type of event which flows from web browser to web-server are called as a client-side events. But if the server sent some updates or information to the browser, then such events are called server-sent events. Hence A server sent event occurs when the browser automatically updated from the Server.

The Server-sent events are mono-directional (always come from server to client). Or it may be called as one-way messaging.

Receiving events from the server

The Server sent event uses the EventSource object to receive events from the server. It specifies the URI of the script which generates the events.

Example:

  1. if(typeof(EventSource) !== "undefined") {  
  2.    var source = new EventSource("ServerUpdate.php");  
  3.   source.onmessage = function(event) {  
  4.     document.getElementById("output").innerHTML += event.data + "<br>";  
  5.   }  

Code Explanation:

  • First, create the new EventSource object, and define the URI of the page which sends server updates. Here we have taken ServerUpdate.php for sending the updates to the web browser.
  • Each time when an update occurs from the server, then the onmessage event occurs and print the message on the web page.
  • The occurred message can be displayed on div using id "output".

Check browser support for Server-sent Event

First we need to check the browser support for server-sent event. So to check the browser support for Server-sent event we will check the EventSource object is true or not. If it is true then it will give alert for supporting else it will give alert for not supporting.

Example:

  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.   <title>HTML5 SSE API</title>  
  5. </head>  
  6. <body>  
  7. <div id="output"></div>  
  8. <script type="text/javascript">  
  9.      
  10.    if(typeof(EventSource)!=="undefined"){  
  11.        alert("Hey! Your browser is supporting.");  
  12.     }  
  13.    else{  
  14.     alert("Sorry! Your browser is not supporting.");   
  15.    }  
  16.  </script>  
  17. </body>  
  18. </html>  

Sending events from the server:

To work with server-sent, we need a server which can send data updates to the web browser. For this, we need to create a file in ASP, PHP or any dynamic language.

Following is the example to show the server updates:

ServerUpdate.php:

Example:

  1. <?php  
  2.  header('Content-Type: text/event-stream');  
  3.  header('Cache-Control: no-cache');  
  4. /Get the current time of server  
  5.   $time = date('r');  
  6.  echo "data: The Current Server time is: {$time}\n\n";  
  7.  flush();  
  8.  ?>  

Code Explanation:

  • In the first line of the code, we have set the "Content-type" header to "text/event-stream". It is required for server-side event standard.
  • The second line informs the server to turn off the caching else the server updates may be cached.
  • echo "data: The Current Server time is: {$time}\n\n"; It creates the output of data to send, and it must always start with data: .
  • Then, we have used the flush () method, which makes sure that data is sent right away to the web page.

Complete Example:

Example:

  1.    <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.   <style type="text/css">  
  5.     div{  
  6.       text-align: center;  
  7.       background-color: #98f5ff;  
  8.     }  
  9.   </style>  
  10. </head>  
  11. <body>  
  12.   
  13. <h1 align="center">Dynamic Server Updates</h1>  
  14. <div id="output"></div>  
  15. <script>  
  16. if(typeof(EventSource) !== "undefined") {  
  17.   var source = new EventSource("ServerUpdate.php");  
  18.   source.onmessage = function(event) {  
  19.     document.getElementById("output").innerHTML += event.data + "<br>";  
  20.   }  
  21. } else {  
  22.   alert("Sorry, your browser does not support the server sent updates");}  
  23. </script>  
  24. </body>  
  25. </html>  

Note: To execute the above code on your browser, you need a server installed on your system, and then run this on localhost. You can install any server such as MYSQL, XAMPP, etc.


Post a Comment

0 Comments

Recents Post

Exploring Paidwork - Your Gateway to Earning Online