-
Notifications
You must be signed in to change notification settings - Fork 64
SSE Method
Since 1.2.2
Server-Sent Event methods are an alternative to POLL methods. They are called by the EventSource object from Javascript.
A Server-Sent Event method is annotated with @ExtDirectMethod(value = ExtDirectMethodType.SSE).
If a SSE method needs to support parameters they have to be annotated with @RequestParam. The value attribute must match the name of the request parameter
and is optional if the code is compiled with debug informations. If there is no value attribute Spring tries to read the name of the parameter from the debug informations.
A SSE method can either return an instance of SSEvent or any object which will be converted to a String with the object's toString() method.
@Service
public class Poll {
@ExtDirectMethod(value = ExtDirectMethodType.SSE)
public SSEvent sse1(@RequestParam int id) {
SSEvent event = new SSEvent();
event.setRetry(10000);
event.setData("some data");
return event;
}
@ExtDirectMethod(value = ExtDirectMethodType.SSE)
public String sse2() {
return "some data";
}
}
The annotation @RequestParam works the same way as in Spring MVC.
The signature of a SSE method can also contain server object parameters (see also Simple method).
@ExtDirectMethod(value = ExtDirectMethodType.SSE)
public SSEvent sse1(HttpServletResponse response, HttpServletRequest request,
HttpSession session, Locale locale,
@RequestParam(value = "id") int id) {
...
}
The parameters may appear in any order and the name of the parameter is irrelevant.
The client uses the EventSource object to communicate with a SSE method. Not all browsers support Server-Sent Events. See supported browsers on CanIuse.com. There are a few polyfills (https://github.com/Yaffle/EventSource and https://github.com/remy/polyfills) that provide implementations that should work in most of the browsers.
api.js will create a variable for each SSE method that contains the URL for the EventSource constructor function.
By default these variables have the form Ext.app.SSE.name_of_the_bean.name_of_the_method.
The example above creates these two variables: Ext.app.SSE.poll.sse1 and Ext.app.SSE.poll.sse2
The code for calling the sse1 method then looks like this:
var es = new EventSource(Ext.app.SSE.poll.sse1);
es.addEventListener('message', sse1MessageHandler, false);
function sse1MessageHandler(event) {
//...
}
More informations about Server-Sent Events: http://www.w3.org/TR/eventsource/ https://developer.mozilla.org/en-US/docs/Server-sent_events/EventSource https://developer.mozilla.org/en-US/docs/Server-sent_events/Using_server-sent_events http://www.html5rocks.com/en/tutorials/eventsource/basics/
- Introduction
- Changelog 1.7.x
- Setup Maven
- Configuration
- Server Methods
- Model Generator
- Model Generator APT
- Development
- Links