My problem is slightly different, but I can describe the issue in the following manner.
What I would like is some code that emits an item every delay period (3 seconds). But when I hit the /flux
URL, the page waits for 3 seconds and gives all 4 items. That means it emits all the items after 3 seconds instead of one item every 3 seconds.
@RestController
@RequestMapping("/flux")
public class MyController {
List<Item> items = Arrays.asList(
new Item("name1","description1"),
new Item("name2","description2"),
new Item("name3","description3"),
new Item("name4","description4"));
@GetMapping(produces = MediaType.TEXT_EVENT_STREAM_VALUE)
Flux<Item> getItems(){
return Flux.fromIterable(items)
.delayElements(Duration.ofSeconds(3));
}
}
@Data
@AllArgsConstructor
class Item{
String name;
String description;
}
I saw this post to explain how to do this in RxJava, so I tried this. But with ZipWith
the results are worse. Now the page waits 12 seconds. That means the browser response is sent only when Flux is completed... Not Sure why.
Flux<Item> getItems(){
return Flux.fromIterable(items)
.zipWith(Flux.interval(Duration.ofSeconds(3)),(item,time)->item);
}
p.s. Using Spring WebFlux dependency, so local started up with Netty not Tomcat.
Problem is not in the code, problem is in the browser. If we use Chrome, Both the code mentioned above works as expected. But does not work with safari browser