Behrooz Nobakht
2014-04-29 18:14:15 UTC
Hi,
Iâve followed the documentation on
SSE<https://jersey.java.net/documentation/latest/sse.html>to implement
a RESTful resource in combination with a JavaScript client.
Simplifying the code, at the level of Jersey resources, I have:
@***@Produces(SseFeature.SERVER_SENT_EVENTS)@Path("out")public EventOutput get(
@HeaderParam(SseFeature.LAST_EVENT_ID_HEADER)
@DefaultValue("-1") String lastEventId) {
final EventOutput output = new EventOutput();
// publish an async mechanism to build the output in a separate thread
return output;
}
and in a separate thread, the event output is being built as:
OutboundEvent.Builder oeb = new OutboundEvent.Builder();
List<MyPojo> events = // retrieve my pojos
listoeb.name("event");oeb.id("someID");
oeb.reconnectDelay(500);
oeb.mediaType(MediaType.APPLICATION_JSON_TYPE);
oeb.data(MyPojo.class, events);try {
OutboundEvent event = oeb.build();
output.write(event);
logger.info("Flushed SSE event: {}", event);
} catch (Exception x) {
logger.error("Failed flushing SSE event: {}", x);
} finally {
try {
output.close();
} catch (Exception x2) {
logger.error("Failed closing output: {}", x2);
}
}
and at the client side, I have a simple JavaScript code to use
EventSource<https://developer.mozilla.org/en-US/docs/Server-sent_events/Using_server-sent_events>as:
var source = new EventSource('//myserver/events/out');
source.addEventListener('event', function(e) {
var jsonData = JSON.parse(e.data);
// use the jsonData
}, false);
The problem starts with the observation that only the first event is
processed and all subsequent events remain in a âpendingâ state of HTTP
request (using Chrome developer console).
Investigating further shows indeed that the header Transfer-Encoding is set
to âchunkedâ. By its own, I think this should not be a problem. However,
there are a number of resources (such as
this<http://stackoverflow.com/q/13590755/248082>,
that<http://www.whatwg.org/specs/web-apps/current-work/multipage/comms.html#authoring-notes>,
or even <https://github.com/http-kit/http-kit/issues/56>) suggest that
Transfer-Encoding should be set to âidentityâ or even removed. Is this
possibly from Jersey server side? Iâm using Jersey with embedded Jetty HTTP
container factory.
It is also an interesting observation based on the above code that I see
consecutive log lines saying âFlushed SSE Event âŠâ but the client side
remains pending and nothing happens further.
I appreciate any feedback or solution to this issue as it seems not to be
so documented or explored.
Thanks in advance,
Behrooz
Iâve followed the documentation on
SSE<https://jersey.java.net/documentation/latest/sse.html>to implement
a RESTful resource in combination with a JavaScript client.
Simplifying the code, at the level of Jersey resources, I have:
@***@Produces(SseFeature.SERVER_SENT_EVENTS)@Path("out")public EventOutput get(
@HeaderParam(SseFeature.LAST_EVENT_ID_HEADER)
@DefaultValue("-1") String lastEventId) {
final EventOutput output = new EventOutput();
// publish an async mechanism to build the output in a separate thread
return output;
}
and in a separate thread, the event output is being built as:
OutboundEvent.Builder oeb = new OutboundEvent.Builder();
List<MyPojo> events = // retrieve my pojos
listoeb.name("event");oeb.id("someID");
oeb.reconnectDelay(500);
oeb.mediaType(MediaType.APPLICATION_JSON_TYPE);
oeb.data(MyPojo.class, events);try {
OutboundEvent event = oeb.build();
output.write(event);
logger.info("Flushed SSE event: {}", event);
} catch (Exception x) {
logger.error("Failed flushing SSE event: {}", x);
} finally {
try {
output.close();
} catch (Exception x2) {
logger.error("Failed closing output: {}", x2);
}
}
and at the client side, I have a simple JavaScript code to use
EventSource<https://developer.mozilla.org/en-US/docs/Server-sent_events/Using_server-sent_events>as:
var source = new EventSource('//myserver/events/out');
source.addEventListener('event', function(e) {
var jsonData = JSON.parse(e.data);
// use the jsonData
}, false);
The problem starts with the observation that only the first event is
processed and all subsequent events remain in a âpendingâ state of HTTP
request (using Chrome developer console).
Investigating further shows indeed that the header Transfer-Encoding is set
to âchunkedâ. By its own, I think this should not be a problem. However,
there are a number of resources (such as
this<http://stackoverflow.com/q/13590755/248082>,
that<http://www.whatwg.org/specs/web-apps/current-work/multipage/comms.html#authoring-notes>,
or even <https://github.com/http-kit/http-kit/issues/56>) suggest that
Transfer-Encoding should be set to âidentityâ or even removed. Is this
possibly from Jersey server side? Iâm using Jersey with embedded Jetty HTTP
container factory.
It is also an interesting observation based on the above code that I see
consecutive log lines saying âFlushed SSE Event âŠâ but the client side
remains pending and nothing happens further.
I appreciate any feedback or solution to this issue as it seems not to be
so documented or explored.
Thanks in advance,
Behrooz