- HTML5 - Web RTC
- HTML5 - Web CORS
- HTML5 - Web Messaging
- HTML5 - IndexedDB
- HTML5 - Web Workers
- HTML5 - Drag & drop
- HTML5 - Microdata
- HTML5 - Geolocation
- HTML5 - Audio & Video
- HTML5 - Canvas
- HTML5 - WebSocket
- HTML5 - Server-Sent Events
- HTML5 - Web SQL Database
- HTML5 - Web Storage
- HTML5 - MathML
- HTML5 - SVG
- HTML5 - Web Forms 2.0
- HTML5 - Events
- HTML5 - Attributes
- HTML5 - Syntax
- HTML5 - Overview
- HTML5 - Home
HTML5 Demo
- HTML5 - Web slide Desk
- HTML5 - Web Worker
- HTML5 - Drag and Drop
- HTML5 - Geo-Location
- HTML5 - Video Players
- HTML5 - Audio Players
- HTML5 - Canvas
- HTML5 - Server Sent Events
- HTML5 - Web Storage
HTML5 Tools
- HTML5 - Color Code Builder
- HTML5 - Online Editor
- HTML5 - Validation
- HTML5 - Modernizr
- HTML5 - Validator.nu Validation
- HTML5 - QR Code
- HTML5 - Velocity Draw
- HTML5 - MathML
- HTML5 - SVG Generator
HTML5 Useful References
- HTML5 - Char Encodings
- HTML5 - Entities
- HTML5 - URL Encoding
- HTML5 - Fonts Reference
- HTML5 - Color Names
- HTML5 - Quick Guide
HTML5 Tag Reference
HTML5 Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
HTML5 - Server Sent Events
Conventional web apppcations generate events which are dispatched to the web server. For example, a simple cpck on a pnk requests a new page from the server.
The type of events which are flowing from web browser to the web server may be called cpent-sent events.
Along with HTML5,
Web Apppcations 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 continuously from your web server to the visitor s browser.The event streaming approach opens a persistent connection to the server, sending data to the cpent when new information is available, epminating the need for continuous polpng.
Server-sent events standardize how we stream data from the server to the cpent.
Web Apppcation for SSE
To use Server-Sent Events in a web apppcation, 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 apppcation which would expect server time.
<!DOCTYPE HTML> <html> <head> <script type = "text/javascript"> /* Define event handpng logic here */ </script> </head> <body> <span id = "sse"> <eventsource src = "/cgi-bin/ticker.cgi" /> </span> <span id = "ticker"> <TIME> </span> </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 ";
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 pne character.
print "Event: server-time ";
Final step is to send event data using Data: tag which would be followed by integer of string value terminated by a new pne character as follows −
$time = localtime(); print "Data: $time ";
Finally, following is complete ticker.cgi written in Perl −
#!/usr/bin/perl print "Content-Type: text/event-stream "; while(true) { print "Event: server-time "; $time = localtime(); print "Data: $time "; sleep(5); }
Handle Server-Sent Events
Let us modify our web apppcation 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> <span id = "sse"> <eventsource src = "/cgi-bin/ticker.cgi" /> </span> <span id = "ticker" name = "ticker"> [TIME] </span> </body> </html>
Before testing Server-Sent events, I would suggest that you make sure your web browser supports this concept.
Advertisements