Search code examples
javajerseyjersey-2.0server-sent-events

Event source pending - Jersey Java server


Some context first. On the server side I'm using Jersey 2.28 and have included jersey-media-sse as a dependency

The server side code looks like this

@GET
@Path("{id}")
@Produces(MediaType.SERVER_SENT_EVENTS)
public void orderUpdates(@PathParam("id") String id, @Context SseEventSink sink) {
    service.subscribe(id, sink, sse);
    OutboundSseEvent initialMessage = sse.newEventBuilder().comment("Connection initiated").build();
    sink.send(initialMessage);
}

Client side code is - in js

this.eventSource = new EventSource(URL);
this.eventSource.onmessage = e => console.log(JSON.stringify(e));

But the connection is always stuck to (pending) status, even though by attaching a debuger I see that the server code is executed.

network console - EventSource stuck on pending

Debugging another project where sse is being used successfully (with the same dependencies), and the break-point being at sink.send(..) I found that the following fields of the SseEventSink are not populated in my case

  • requestScope
  • requestContext
  • responseContext
  • connectionCallback

enter image description here

I still have not figured out why though, or even if that is the reason that the request is stuck on pending.


Solution

  • Turns out the issue was caused by the GZip filter which was enabled on the servlet and was matching the SSE endpoint.

    Disabling the filter fixes the issue and it behaves as expected.