Wednesday, October 3, 2018

Micronaut Mastery: Consuming Server-Sent Events (SSE)

Normally, we would consume server-sent events (SSE) in a web browser, but we can also consume them in our code on the server. Micronaut has a low-level HTTP client with a SseClient interface that we can use to get server-sent events. The interface has an eventStream method with different arguments that return a Publisher type of the Reactive Streams API. We can use the RxSseClient interface to get back RxJava2 Flowable return type instead of Publisher type. We can also use Micronaut's declarative HTTP client, which we define using the @Client annotation that supports server-sent events with the correct annotation attributes.

In our example, we first create a controller in Micronaut to send out server-sent events. We must create method that returns a Publisher type with Event objects. These Event objects can contains some attributes like id and name, but also the actual object we want to send:



from DZone.com Feed https://ift.tt/2DStFbI

No comments:

Post a Comment