পৃষ্ঠাসমূহ

Search Your Article

CS

 

Welcome to GoogleDG – your one-stop destination for free learning resources, guides, and digital tools.

At GoogleDG, we believe that knowledge should be accessible to everyone. Our mission is to provide readers with valuable ebooks, tutorials, and tech-related content that makes learning easier, faster, and more enjoyable.

What We Offer:

  • 📘 Free & Helpful Ebooks – covering education, technology, self-development, and more.

  • 💻 Step-by-Step Tutorials – practical guides on digital tools, apps, and software.

  • 🌐 Tech Updates & Tips – simplified information to keep you informed in the fast-changing digital world.

  • 🎯 Learning Support – resources designed to support students, professionals, and lifelong learners.

    Latest world News 

     

Our Vision

To create a digital knowledge hub where anyone, from beginners to advanced learners, can find trustworthy resources and grow their skills.

Why Choose Us?

✔ Simple explanations of complex topics
✔ 100% free access to resources
✔ Regularly updated content
✔ A community that values knowledge sharing

We are continuously working to expand our content library and provide readers with the most useful and relevant digital learning materials.

📩 If you’d like to connect, share feedback, or suggest topics, feel free to reach us through the Contact page.

Pageviews

Thursday, February 23, 2017

HTML5 - Server Sent Events

Conventional web applications generate events which are dispatched to the web server. For example a simple click on a link requests a new page from the server.
The type of events which are flowing from web browser to the web server may be called client-sent events.

Along with HTML5, WHATWG Web Applications 1.0 introduces events which flow from web server to the web browsers and they are called Server-Sent Events (SSE). Using SSE you can push DOM events continously from your web server to the visitor's browser.
The event streaming approach opens a persistent connection to the server, sending data to the client when new information is available, eliminating the need for continuous polling.
Server-sent events standardizes how we stream data from the server to the client.

Web Application for SSE

To use Server-Sent Events in a web application, you would need to add an <eventsource> element to the document.
The src attribute of <eventsource> element should point to an URL which should provide a persistent HTTP connection that sends a data stream containing the events.
The URL would point to a PHP, PERL or any Python script which would take care of sending event data consistently. Following is a simple example of web application which would expect server time.
<!DOCTYPE HTML>
<html>
   <head>
   
      <script type="text/javascript">
         /* Define event handling logic here */
      </script>
      
   </head>
   <body>
 
      <div id="sse">
         <eventsource src="/cgi-bin/ticker.cgi" />
      </div>
  
      <div id="ticker">
         <TIME>
      </div>
  
   </body>
</html>

Server Side Script for SSE

A server side script should send Content-type header specifying the type text/event-stream as follows.
print "Content-Type: text/event-stream\n\n";
After setting Content-Type, server side script would send an Event: tag followed by event name. Following example would send Server-Time as event name terminated by a new line character.
print "Event: server-time\n";
Final step is to send event data using Data: tag which would be followed by integer of string value terminated by a new line character as follows −
$time = localtime();
print "Data: $time\n";
Finally, following is complete ticker.cgi written in perl −
#!/usr/bin/perl

print "Content-Type: text/event-stream\n\n";

while(true){
   print "Event: server-time\n";
   $time = localtime();
   print "Data: $time\n";
   sleep(5);
}

Handle Server-Sent Events

Let us modify our web application to handle server-sent events. Following is the final example.
<!DOCTYPE HTML>
<html>
   <head>
 
      <script type="text/javascript">
         document.getElementsByTagName("eventsource")[0].addEventListener("server-time", eventHandler, false);
   
         function eventHandler(event)
         {
            // Alert time sent by the server
            document.querySelector('#ticker').innerHTML = event.data;
         }
      </script>
  
   </head>
   <body>
 
      <div id="sse">
         <eventsource src="/cgi-bin/ticker.cgi" />
      </div>
  
      <div id="ticker" name="ticker">
         [TIME]
      </div>
  
   </body>
</html>
Before testing Server-Sent events, I would suggest to make sure if your web browser supports this concept.

No comments:

Post a Comment