Hot versus Cold publishers

In previous chapters, we built publishers that would start publishing data to each of the subscriber instances after subscription. The Fibonacci publisher that we created in Chapter 2The Publisher and Subscriber APIs in a Reactor, would publish the complete Fibonacci series to each of its subscribers.

Consider the following Fibonacci code as a cold publisher:

Flux<Long> fibonacciGenerator = Flux.generate(        () -> Tuples.<Long, Long>of(0L, 1L),        (state, sink) -> {            sink.next(state.getT1());            return Tuples.of(state.getT2(), state.getT1() + state.getT2());        });fibonacciGenerator.take(5).subscribe(t -> System.out.println("1. "+t));fibonacciGenerator.take(5).subscribe(t -> System.out.println("2. "+t));

Publishers that start ...

Get Hands-On Reactive Programming with Reactor now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.