Abhishek Mishra
Abhishek Mishra

Reputation: 31

How to Read SSE event stream at server side instead of browser

I am creating an application to read Event Stream and keeps the information on my database.

I have an URL: http://xx.xx.xx.xx:8080/restconf/streams/mno-vnf-event/json headers required: Accept:text/event-stream, Authorization:Basic YWRtaW46o99RtaW4-, Content-Type:application/yang-data+json

Now I want to fetch stream from above API using java (not javascript code), anyone has the idea to accomplish this.

Upvotes: 2

Views: 3543

Answers (2)

ctranxuan
ctranxuan

Reputation: 815

If the SSE server follows strictly the SSE spec, you can't add any custom headers.

Nonetheless, if it does, have you tried something like that with Spring WebFlux Client:

ResolvableType type = forClassWithGenerics(ServerSentEvent.class, String.class);

WebClient client = WebClient.create();
client.get()
      .uri("http://localhost:8080/sse")
      .accept(TEXT_EVENT_STREAM)
      .header("MyHeader", "Foo") // Add your headers here.
      .exchange()
      .flatMapMany(response -> response.body(toFlux(type)))
      .subscribe(System.out::println,
                 Throwable::printStackTrace);

I have tested this piece of code against a SSE Undertow server. It seems to work: Undertow is able to get the custom header that has been sent.

Upvotes: 1

ctranxuan
ctranxuan

Reputation: 815

You can use some Java libraries that supports SSE:

These are the first that come to my mind, but there are undoubtedly more.

Upvotes: 2

Related Questions