It’s really hard visualising streams and what operators do when you’re learning reactive programming. Things seem to be unnecessarily complicated, which can demotivate some people to keep studying.
One thing that really helped me understand was to implement streams using the different kind of constructors, applying operators, check the results and comparing with others. And that’s why I created this guide. It contains some “questions” and implemented examples which can help you start your reactive programming “journey”.
This guide is not intended to replace the documentation or teach reactive programming. If you haven’t read anything about it yet, here are some good starting points:
- The introduction to Reactive Programming you’ve been missing
- RxMarbles
- Expert to Expert: Brian Beckman and Erik Meijer - Inside the .NET Reactive Framework (Rx) - YouTube
The questions and examples on this guide are based on the Reactor implementation. And I built it based on this gist.
Well, here are my suggestions on how to use this guide:
- Move to the next question. If you don’t know the answer, try to figure out what would be the answer based on your current context and naming of the classes or methods.
One thing that might help you figure out what a method is supposed to do, is reading its signature: the arguments’ names and types, and return’s type. Actually, this tip is valid for other frameworks and languages.
- Check the example related to that question, and try to understand the source code written for it. Think about what would be its output.
- Uncomment the example method on
App.kt
and run the project
$ ./gradlew run
- Check what was printed on the console, and see if your answer was correct. Otherwise, try to answer it again.
- (Optional) Try to draw a visual representation of it - RxMarbles
Just open an issue if you have an idea on to improve it! Things I missed or it might not be important to know at the beginning of your studies. Questions or example are not really clear. I’d love to hear from you 😁
- what is a publisher?
Mono
xFlux
- when which one of those publishers end?
- how to subscribe to then?
There are some methods called
doOn...
available when applying operators to streams. Those methods are operators, and not a way of subscribing to streams. They’re supposed to be used as “side effects” of a stream, like logging, metrics and debug. Notice thatReactiveUtils.debug
method is implemented using then! I think this an important thing to mention because, maybe you face a methoddoOnError
and think you should use it to handle error emissions, but it should not be used for that. It won’t capture the error, which means the error will be thrown if you won’t have any error handler. One good example isPublishers.kt:57
, which has thedoOnError
operator added to it, but the error stills being emitted.
Mono:
Mono.just
Mono.empty
xMono.justOrEmpty
Mono.error
Mono.never
.toMono()
Mono.delay
Flux:
Flux.just
Flux.empty
Flux.never
Flux.fromIterable
.toFlux()
Flux.interval
can you guess why
Mono
has a constructor calleddelay
andFlux
has one calledinterval
instead?
There’s a constructor called defer
which is available for both: Mono
and Flux
which I think it is worth to spend some time trying to figure out what it does and what would be a good use of it.
- What is a subscription?
- What is its purpose?
Those are not all the existing operators, just some that I think it is worth to having a look. Notice that most of them are only available for
Flux
publishers.
Combinations:
concat
xmerge
combineLatest
xwithLatestFrom
zip
Filters:
distinct
xdistinctUntilChanged
sample
skip
xskipUntil
xskipWhile
Transformations:
buffer
xbuffer(int)
xcollectList
map
xflatMap
xconcatMap
xexpand
scan
xreduce
expand
And here are some you can try implement by yourself
take
xtakeUntil
xtakeWhile
takeLast
xlast
xelementAt
ignoreElements
count
- (
Mono
andFlux
) xConnectableFlux
ReplayProcessor
- what are schedulers?
- what kind of schedulers are available?
publishOn
xsubscribeOn
parallel
andrunOn
checkpoint
ReactiveUtils.debug
StepVerifier