Horje

Code Explanation: Server-Sent Events - One Way Messaging

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:

ServerUpdate.php
Example: HTML
       <!DOCTYPE html>  
    <html>  
    <head>  
      <style type="text/css">  
        div{  
          text-align: center;  
          background-color: #98f5ff;  
        }  
      </style>  
    </head>  
    <body>  
      
    <h1 align="center">Dynamic Server Updates</h1>  
    <div id="output"></div>  
    <script>  
    if(typeof(EventSource) !== "undefined") {  
      var source = new EventSource("ServerUpdate.php");  
      source.onmessage = function(event) {  
        document.getElementById("output").innerHTML += event.data + "<br>";  
      }  
    } else {  
      alert("Sorry, your browser does not support the server sent updates");}  
    </script>  
    </body>  
    </html>  





Type:
html
Category:
Web Tutorial
Sub Category:
HTML SSE API
Uploaded by:
Admin