Thursday, July 25, 2013

SERVER SENT EVENTS

SERVER SENT EVENTS 



Server-sent events is a technology for where a browser gets automatic updates from a server via HTTP connection. The Server-Sent Events EventSource API is standardized as part of HTML5 by the W3C.

Examples: Facebook/Twitter updates, stock price updates, news feeds, sport results, etc.



Web browser support for Server-Sent Events
Browser
Supported
Notes
NO
[3]
Yes
Starting with Firefox 6.0 [4]
Yes
[3]
Yes
Starting with Opera 11 [3]
Yes
Starting with Safari 5.0 [3]



SERVER SENT EVENTS IN SPRING MVC

Controller for mapping events

@Controller
@RequestMapping("sseTest.htm")
public class SseController {
@RequestMapping(method = RequestMethod.GET)
public @ResponseBody String sendMessage(HttpServletResponse response) {
                Random r = new Random();
                response.setContentType("text/event-stream, charset=UTF-8");
                try {
                        Thread.sleep(10000);
                } catch (InterruptedException e) {
                        e.printStackTrace();
                }   
                return "data:Testing 1,2,3" + r.nextInt() +"\n\n" + "retry: 100\n" ;
      }

}

Controller for mapping jsp page

@Controller
@RequestMapping("viewSSE.htm")
public class ViewSSEController {
 
@RequestMapping(method = RequestMethod.GET)
public String home(ModelMap model){
 
 return "viewSSE";
}
}


In view (viewSSE.jsp)

No comments:

Post a Comment