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
serverSentEvents:function(){
if(typeof(EventSource) !== "undefined") {
var source = new EventSource("/context/getNotificationCount?tableId="+selectedTableInfo.params.tableId); // create the EventSource object
source.onmessage = function(event) { //event listener for the event source
console.log(event.data) // 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)
@ResponseBody
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;
}
