Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

who calls subscribe on Flux or Mono in reactive webapplication

I am looking at some examples of reactive web applications and i am seeing them like this

@RequestMapping(value = "/{id}", method = RequestMethod.GET)
@ResponseBody    
public Mono<Person> findById(...) {
    return exampleService.findById(...);
}

@RequestMapping(method = RequestMethod.GET, produces = MediaType.TEXT_EVENT_STREAM_VALUE)
@ResponseBody
public Flux<Person> findAll() {
    Flux<Person> persons = exampleService.findAll();
    return persons;
}

When i am reading about the Mono and Flux in the documentation it mentioned subscribe has to be called for Mono or Flux to emit the data.

So when i run these reactive webapplications locally and using postman/chrome browser when i hit the endpoints i getting the results.

On the service side though endpoints are returning Mono or Flux, how i am seeing the actual results in the browser/postman. Is the browser doing the part of calling the subscribe internally whenever i am hitting the endpoints that return Mono/Flux types?

like image 356
vjk Avatar asked Jun 07 '19 02:06

vjk


People also ask

What is mono and flux in reactive programming?

Mono and Flux are both implementations of the Publisher interface. In simple terms, we can say that when we're doing something like a computation or making a request to a database or an external service, and expecting a maximum of one result, then we should use Mono.

What is subscribe in reactive programming?

The Subscribe operator is the glue that connects an observer to an Observable. In order for an observer to see the items being emitted by an Observable, or to receive error or completed notifications from the Observable, it must first subscribe to that Observable with this operator.

What is mono and flux in WebFlux?

Project Reactor is the implementation of Reactive Streams specification. Reactor provides two types: Mono: implements Publisher and returns 0 or 1 elements. Flux: implements Publisher and returns N elements.

What is subscribe in reactive spring?

In the Reactive Streams API there are four main interfaces: Publisher — Emits events to subscribers based on the demands received from its subscribers. A publisher can serve multiple subscribers and it has only one method: subscribe. Subscriber — Receives events emitted by the Publisher.


2 Answers

Mono and Flux concepts exist only within your application, while HTTP protocol is used to communicate between your postman/chrome app and your application.
Internal classes of the Spring Webflux framework subscribe to Mono and Flux instances returned by your controller methods and map them to HTTP packets based on the MediaType that you specified in RequestMapping.

like image 145
Ilya Zinkovich Avatar answered Sep 24 '22 00:09

Ilya Zinkovich


It depends on which server you use.

For instance, Tomcat, Jetty (Servlet 3.1 non-blocking I/O) - ServletHttpHandlerAdapter from org.springframework.http.server.reactive package.

Subscription happens in service method:

@Override
public void service(ServletRequest request, ServletResponse response) throws 
  ServletException, IOException {        
    ...
    HandlerResultSubscriber subscriber = new HandlerResultSubscriber(asyncContext, 
        isCompleted, httpRequest);
    this.httpHandler.handle(httpRequest, httpResponse).subscribe(subscriber);
}
like image 37
Yauhen Balykin Avatar answered Sep 22 '22 00:09

Yauhen Balykin