Horje
How to add Server-Sent Events - One Way Messaging | HTML SSE API Code
How to add Server-Sent Events - One Way Messaging

A server-sent event is when a web page automatically gets updates from a server.

This was also possible before, but the web page would have to ask if any updates were available. With server-sent events, the updates come automatically.

Examples: Facebook/Twitter updates, stock price updates, news feeds, sport results, etc.


Example of 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
    <?php  
     header('Content-Type: text/event-stream');  
     header('Cache-Control: no-cache');  
    /Get the current time of server  
      $time = date('r');  
     echo "data: The Current Server time is: {$time}\n\n";  
     flush();  
     ?>  

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>  

Reffered: https://www.javatpoint.com/html-server-sent-event



Read Full::
HTML SSE API Code
Category:
HTML Code
Sub Category:
HTML SSE API Code
Uploaded:
1 month ago
Uploaded by:
Admin
Views:
1

Related Articles
What is HTML SSE API HTML SSE API Code
How to add Server-Sent Events - One Way Messaging HTML SSE API Code
What types of browsers will support HTML SSE API HTML SSE API Code
How to Receive Server-Sent Event Notifications HTML SSE API Code
How to Check Server-Sent Events Support HTML SSE API Code
What is Server-Side Code Example HTML SSE API Code
What are The EventSource Object HTML SSE API Code

Single Articles
Example of Server-Sent Events - One Way Messaging
Code Explanation: Server-Sent Events - One Way Messaging

Share on: