Skip to content
Advertisement

Debugging server-sent events stream from Spring in Chrome and Postman

According to the Spring documentation, when returning a Flux, Spring should emit a server-sent event for each element returned by the subscription.

Here’s an exemplaric REST controller:

JavaScript

Example response, using wget:

JavaScript

When debugging the endpoint using Chrome or Postman, the clients seem to interpret the events as parts of a chunked response – not as a stream of events. I have confirmed that the response data is the same, and requires the expected amount of time. See screenshots from Chrome Network tab below:

Chrome Developer Networking Tab -> Headers

The EventStream – tab is empty:

Chrome Developer Networking Tab -> EventStream Chrome Developer Networking Tab -> Timing


Compare this to the headers of a website like http://www.emojitracker.com/:

Chrome Developer Networking Tab -> Headers

Where the EventStream tab displays the events correctly:

Chrome Developer Networking Tab -> EventStream


The important fact is, when consuming the Spring endpoint with a WebClient, I can successfully receive each event at the expected time using .bodyToFlux. So it seems like the events are missing some form of configuration that Chrome expects from a server-sent event stream – but which?

Advertisement

Answer

First of all in the browser when using EventSource will perform a GET request so using a PATCH is not really compatible in the browser.

If we takes your code and change the PATCH in GET and we create a simple page page:

JavaScript

Make your application serve this static file and open it in chrome. You will correctly see the events in the Event tab. However if you directly request /api/v1/test you will this the event in the page but not in the Event tab. I suppose that the event tab intercept EventSource object and won’t be used if no EventSource is created.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement