I haven't tested this yet, since I'm a little busy - I'll do it later if that doesn't work. :)
From the above comments, it seems that the problem is with creating Flux.
I assume Spring Reactive controllers can handle Flux, which emits multiple, without using WebSockets or SSE. Again, I will have a game a bit later.
Flux has many static building methods that will help you here.
How to do it as follows:
return Flux.intervalMillis(1000) .map(l -> new new SensorRead(sensorId, Math.random()));
But it will give you an endless stream, which may not be what you want.
Another option looks something like this:
return Flux.range(1, 5) //Spit out 5 values starting from 1 .delayMillis(1000) //Delay the onNext calls to separate 1 second apart .map(l -> new new SensorRead(sensorId, Math.random()));
Update
OK, so this question has changed significantly.
In response to "Why can't we call onNext () several times? How can we do this?"
Of course, I didn’t write an API, so the arguments that I can’t answer cannot answer, but IMO has ambiguity and complexity as to how to handle multiple outliers in a myriad in different ways that can be expressed,
HTTP 1.1 does not allow multiple responses to a request, so the only acceptable option is some collect in the list or at a low level, writing onNext to the output stream for each emission - both of which have difficulties around the content type (EG XML vs. JSON)
This is even more complicated when we present HTTP2, WebSockets and SSE, which can execute several forms of response on request - each time when it needs to be processed differently.
If you want to make multiple outliers, you need to look at WebSockets or SSE.
There are SSE classes in the Spring -Reactive project, so it looks like it is implemented.
E.G.
@RequestMapping("/sse/event") Flux<SseEvent> sse() { return Flux.interval(Duration.ofMillis(100)).map(l -> { SseEvent event = new SseEvent(); event.setId(Long.toString(l)); event.setData("foo"); event.setComment("bar"); return event; }).take(2); }
Take a look at the examples below:
https://github.com/spring-projects/spring-reactive/blob/master/src/test/java/org/springframework/web/reactive/result/method/annotation/SseIntegrationTests.java
Hope this helps