Server sent events API


Server sent events API API is used for opening an HTTP connection to receive push notifications from a server. These events are received as DOM events.
The server-sent event API is contained in the EventSource interface; to open a connection to the server to begin receiving events from it, you create a new EventSource object, specifying the URI of a API that generates the events.
Examples: Notifications, Facebook/Twitter updates, stock price updates, news feeds, sport results, etc.

JS code

	if(typeof(EventSource) !== "undefined") {
          // create the EventSource object
	  var source = new EventSource("/context/getNotificationCount?tableId="+selectedTableInfo.params.tableId); 
	  source.onmessage = function(event) { //event listener for the event source
	   console.log( // server side message is rturned as the data attribute in the event object
	} else {
		console.log("Sorry, your browser does not support server-sent events...");

Service code:

@RequestMapping(value = "/getNotificationCount", method = RequestMethod.GET)
public String getAuditNotificationCountSS(HttpServletRequest request, HttpServletResponse response, @RequestParam(UDMContollerConstants.TABLE_ID) Integer tableId) throws UDM3GenericException {
	String count = null;
	count = serviceHelpwe.getAuditNotificationCount(tableId);
	return "data: "+count;

Posted in HTML5, Web Technology Tagged with: , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *