WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

HTML5 Server-Sent Events


In this section we learn Server Sent Events. it is exceptionally useful permit page to get refresh from server.


Server-Sent Events - One Way Messaging

Server side occasion website page programmed gat refresh from server.

Server side occasion refresh are very populer this field, for example, Facebook Twitter News channel stock value sport result and so forth


Browser Support

The numbers in the table determine the principal program form that completely bolster server-sent events.

API
SSE 6.0 Not supported 6.0 5.0 11.5

Receive Server-Sent Event Notifications

The EventSource object is utilized to get server-sent occasion notifications:

Example

var source = new EventSource("test_file.php");
source.onmessage = function(event) {
    document.getElementById("result").innerHTML += event.data + "<br>";
};
Try it yourself »

Example explained:

  • Create another EventSource object, and determine the URL of the page sending the updates (in this precedent "test_file.php")
  • Each time a refresh is gotten, the onmessage occasion occurs
  • When an onmessage occasion happens, put the got information into the component with id="result"

Check Server-Sent Events Support

In the tryit precedent above there were some additional lines of code to check program support for server-sent events:

if(typeof(EventSource) !== "undefined") {
   /Yes! Server-sent occasions support!
   /Some code.....
} else {
   /Sorry! No server-sent occasions support..
}

In this area we learn Server Sent Events. it is especially valuable license page to get revive from server.


Server-Sent Events - One Way Messaging

Server side event site page customized gat revive from server.

Server side event revive are very populer this field, for instance, Facebook Twitter News channel stock esteem sport result, etc


Browser Support

The numbers in the table decide the foremost program structure that totally reinforce server-sent events.

API
SSE 6.0 Not supported 6.0 5.0 11.5

Receive Server-Sent Event Notifications

The EventSource object is used to get server-sent event notifications:

Example

var source = new EventSource("test_file.php");
source.onmessage = function(event) {
    document.getElementById("result").innerHTML += event.data + "<br>";
};
Try it yourself »

Example explained:

  • Create another EventSource object, and decide the URL of the page sending the updates (in this point of reference "test_file.php")
  • Each time a revive is gotten, the onmessage event occurs
  • When an onmessage event occurs, put the got data into the segment with id="result"

Check Server-Sent Events Support

In the tryit point of reference above there were some extra lines of code to check program support for server-sent events:

if(typeof(EventSource) !== "undefined") {
   /Yes! Server-sent events support!
   /Some code.....
} else {
   /Sorry! No server-sent events support..
}