Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Server-Sent Event Client example in Spring

Our team has developed Server-Sent events (SSE) for a specific task.

I am trying to build a client to listen to event streams from our server. I have sort of managed to do this using the Jersey libraries for Java. However since most of our client code uses Spring, I would like to have an example of how this could be done using Spring.

I was able to find many examples on SSE on the server side for Spring. However I am unable to find any documentation for the client side.

Does Spring support SSE on the client side? If yes, may I have an example of how the following can be achieved using Spring...

Send an HTTP GET request to our server...
GET -> http://example.com/api/events/
headers ->
Accept:text/event-stream
sessionKey:someString

The response will be a text stream, that the client will continue to receive, until the client chooses to close the connection.

like image 581
Newton Raphson Avatar asked Mar 29 '17 00:03

Newton Raphson


People also ask

What is server sent events in spring boot?

Simply put, Server-Sent-Events, or SSE for short, is an HTTP standard that allows a web application to handle a unidirectional event stream and receive updates whenever server emits data. Spring 4.2 version already supported it, but starting with Spring 5, we now have a more idiomatic and convenient way to handle it.

What is SSE Java?

Server-Sent Events (SSE) is an HTTP based specification that provides a way to establish a long-running and mono-channel connection from the server to the client. The client initiates the SSE connection by using the media type text/event-stream in the Accept header.

How do server sent events work?

So what are Server-Sent Events? A client subscribes to a “stream” from a server and the server will send messages (“event-stream”) to the client until the server or the client closes the stream. It is up to the server to decide when and what to send the client, for instance, as soon as data changes.


1 Answers

Have a look at one of the execute methods of the Spring RestTemplate. They all take a ResponseExtractor as parameter. This callback interface defines one method: extractData(ClientHttpResponse response). By providing your own ResponseExtractor you can do what you want with the response, like reading from it line-by-line. Naive example:

restTemplate.execute(a_url, HttpMethod.GET, request -> {
        }, response -> {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response.getBody()));
            String line;
            try {
                while ((line = bufferedReader.readLine()) != null) {
                    System.out.println("Got some data, let's use my ObjectMapper to parse into something useful!");
                }
            } catch (IOException e) {
                //Something clever
            }
            return response;
        });
like image 144
Svante Avatar answered Sep 30 '22 21:09

Svante