HTML SSE ( Server Sent Events) – Listen to server from HTML

0
15

Image

 

Server sent Event is event when getting informed aboout Data updation from server, Facebook , Twitter updates are working in this way. Here is how it is

See the Entire HTML Code

<html>
<head>
<script>
function listtime()
{

if(typeof(EventSource)!==”undefined”)
{
// Yes! Server-sent events support!

var source=new EventSource(“time.asp”);

/*

source.onopen=function(event)
{
document.getElementById(“result”).innerHTML=”Opened..”;
};

source.onerror=function(event)
{
document.getElementById(“result”).innerHTML=”Error :” + event.error;
};

*/

source.onmessage=function(event)
{
document.getElementById(“result”).innerHTML= event.data;
};

}
else
{
// Sorry! No server-sent events support..
document.getElementById(“result”).innerHTML+=”NO SSE SUPPORT”;

}

}

</script>
<title>SSE Test</title>
</head>
<body onload=”listtime();”>
<div id=”result”>Time Here</div>
</body>
</html>

Here unline listening to server with javascript code we can straightly code to listen for event right from HTML

Functions in Detail

onopen

onmessage

onerror

all the functions are self explanatory by respective names, like

onopen :- trigger when connection started

onmessage :- when ever there is an updation on server page

onerror :- when an error occured

To use this code just make a divided with id=”result”, when ever there is a change the divider will get populated with the function code

SHARE
Previous articleFacebook , Twitter , Google plus Graphics Image Sizes
Next articleHTML5 App Cache- Speed Up your Website
abhilash, is a programmer by profession and a technology addict with a passion towards browsing , shopping and imaging . My views may be biased on reviews. all images and videos are copyrighted to respective owners.

LEAVE A REPLY

Please enter your comment!
Please enter your name here