I recently ran a workshop on Rx, and as part of preparing I had to hunt down some useful resources for learning. I found that things had come a long way since the early days, when people mostly learned from videos of Wes Dyer, Jeff van Gogh and Bart de Smet drawing on whiteboards at Microsoft, and Lee Campbell’s blog.
Since the initial .Net release, a lot has changed for Rx – it’s become open source, and it’s now available for many platforms: .Net, Java, Javascript, plus quite a few more. It’s also being used and talked about by some big players outside Microsoft, like NetFlix (who are responsible for the RxJava port).
With all this new exposure, there are now a lot more resources for learning Rx. Here are some that I think are particularly useful.
Intro to Rx
This is Lee Campbell’s free book, which has a thorough coverage of Rx, from a .Net angle. If you’re not using the .Net version of Rx it’s still worth a read – there are a lot of valuable explanations of the core concepts and operators, which are basically common to all implementations of Rx.
If you’re looking for a resource on Rx to sit down and read cover to cover, this is the best one I know.
ReactiveX
If you’re looking at all the operators in Rx and wondering how you’re supposed to know when to use them, ReactiveX has some good info on categories of operators. There’s even a decision tree that points you in the right direction when you’re thinking something like “I want to make a single stream out of the elements from two streams”.
Many Rx operators have multiple versions that behave slightly differently depending on the arguments you pass in. ReactiveX’s operator pages have marble diagrams and explanations showing how each of the cases work. It also shows you what the operator is called on each of the supported platforms.
For example, the map operator page shows you that Rxjs has a related operator called ‘pluck’, and you can see marble diagrams and examples for both ‘map’ and ‘pluck’. It also shows that .Net’s name for ‘map’ is ‘Select’.
All the aliases for an operator point to the same page, so you can search for the name you’re familiar with. ReactiveX does tend to have more detail for Rxjs, though.
Rxjs book
Mostly the Rxjs book just republishes the Rxjs doc pages on Github, though perhaps with a nicer, more explorable interface. What I like about the Rxjs book is that it uses jsBin to make the examples interactive. Running the examples live means you can see how they execute in real time, and change the code to test different scenarios. This can be really handy for getting a feel for how operators behave, especially time based ones.
For example, the examples for bufferWithTime and bufferWithCount show how the buffers appear to you, given a certain stream of input over time. They also show you the difference between overlapping buffers and non-overlapping buffers:
Using this as a test harness, you can try a few things to get a feel for how buffering works:
- Change the buffer parameters to have more or less items/time in each buffer
- Try out different inputs – empty streams, slower/faster streams, or maybe quiet periods where the input stops producing values temporarily
- Change the buffer overlap
- See what happens when the input errors or completes
When you’re combining different source streams, or doing operations based on time, being able to tweak the example’s source data or timing is a great way to get a better understanding of what’s going on.
RxMarbles
This is a fantastic project that gives you interactive marble diagrams, which can be super helpful in understanding how the operators work. Both ReactiveX and the Rxjs book use RxMarbles on their operator pages, which works well by giving you an interactive example with the documentation.
If you haven’t come across marble diagrams, they’re a way of showing input events and output events on streams to demonstrate what an operator does. Separate lines show events on the inputs and the output, with time flowing from left to right in the diagram.
For example, below is a static marble diagram showing how the merge operator merges two streams. What makes RxMarbles cool is that you can drag the input values around to see how it would affect the result stream. Follow the link to try changing the inputs to see the effect on the result.
RxMarbles is a work in progress, so some operators aren’t covered yet. But many of the fundamentals are there, so it’s a good place to get to know the operators you’ll be using most commonly.
LinqPad
If you’re using the .Net version of Rx, LinqPad is a must. This tool is so useful for .Net code in general, it’s almost a crime not to use it. But specifically for Rx, it’s great. Every time I want to test a particular Rx scenario, I jump straight into Linqpad.
LinqPad has a method called ‘Dump’ which can make a useful display of just about anything you throw at it. For Rx streams, or Observables, it will show you a live display of what’s coming out of the stream. If your stream is a stream of rich data, where each item has lots of sub objects, LinqPad displays the whole object graph neatly.
For an example, we’ll use a quick bit of code that gets an incrementing integer every second (Observable.Interval), and groups them by into odd or even (GroupBy). It’s a simple example, but it can be very powerful if you’re learning what an operator does, or if you have a stream of complex source data.
Here’s the example we’ll run:
Observable.Interval(TimeSpan.FromSeconds(1)) .GroupBy(i => i % 2) .Dump();
And here’s LinqPad’s output. It shows us that we’ve split the source stream into two streams – a stream of odd numbers and a stream of even numbers:
If you’re using Rxjs, the closest thing to LinqPad would be to find the Rxjs book page for the operator you’re interested in, then alter the sample code to match your scenario.
Tutorials
Tutorials can be a great way to learn, but it really depends on the quality and depth of the tutorial. Getting hands on is always a good thing for learning, which is why the interactivity of LinqPad and the RxJS book examples are great learning tools. Creating a thorough tutorial takes a lot of time, so there aren’t many out there. Smaller tutorials can be good for getting a feel for a particular part of the framework, but sometimes they leave you with more questions than answers.
If you’re interested in some tutorials, check out some of these. Once you’re done, if you’re looking for more info, try some of the resources in this post.
Functional Programming in Javascript – Jafar Husain
Jafar Husain’s tutorial is one of the few thorough ones out there. It spends a lot of time going through functional array filtering/manipulation, which may leave you a little nonplussed when you’re looking for an Rx tutorial. But it builds your understanding of applying a functional approach to data so that when you hit the Rx examples, you already understand it. It’s a good approach – I think most of the learning curve for Rx is in its functional style api, rather than dealing with streams. Arrays are familiar and the tutorial may leave you never wanting to foreach through an array again. In which case, dig into Lodash, Underscore or IxJS.
The introduction to Reactive Programming you’ve been missing – Andre Staltz
This is another tutorial that’s worth a read. It doesn’t dive as deep on the functional programming side as Jafar Husain’s tutorial, he focuses more on talking through each of his points. This tutorial will give you a walkthrough of some cases where Rx is useful and how it applies. Then go back to Jafar Husain’s tutorial to really get some hands on feeling for how it works.
My Rx Workshop
The Rx workshop I ran for Women Who Code is on github. It was designed as an in person workshop more than an online tutorial, but the page is fairly self explanatory and the exercises are useful for getting a feel for what you can do with Rx.
It starts with a short set of exercises to get you familiar with common stream operations, and moves on to applying these operations to a tweet stream to build a real-time dashboard about the stream. This shows you how you can tie together just a few operators to generate information that can drive business features. For example, it’s fairly easy to identify sudden spikes in twitter traffic on a particular topic, which may indicate some real world event has just happened.
Real world examples
Getting real world examples of applications of Rx can be a little tricky. To give a real world example you need a real world problem that’s big enough or complex enough to benefit from Rx, but simple and small enough to distill down to something useful.
I’ve written a few posts about real world examples of applying Rx:
- Replaying time series data in real-time at any arbitrary speed. This can be used to recreate historical streams (eg a tweet stream, event sourced data, stock data), which can even be used for creating load tests based on real production data.
- Monitoring the health of service dependencies. This is a useful tool when integrating many services together to create a fault tolerant system that can recover as dependencies fail or are restarted/updated.
- Reacting to quiet periods in a variable rate stream. This example shows how to detect cases where a stream of continuous data (eg a stock prices stream) suddenly goes quiet and then later resumes. It also shows how you can react and change the system behaviour as the underlying stream changes state.
RxJava
RxJava is a port from Rx.Net created by NetFlix, and there are some interesting videos and presentations about what Rx did for them. RxJava works on the JVM, so it’s also there for Scala, Groovy, Clojure, etc.
The RxJava doc is mostly a set of links to the ReactiveX site, but it’s still a useful resource for anyone using RxJava, as its format/structure make for easy reading, and points you to the right places on the ReactiveX site to dig deeper. There’s also a good page on backpressure, which deals with the situation when a stream is faster than its subscribers.
Mastering Observables in the Couchbase documentation has some good information and goes through creating, transforming, filtering and merging streams.
Grokking RxJava is a series of three posts from Dan Lew which runs through an introduction to RxJava. There’s an extra post at the end on RxAndroid.
Rxjs doc
The Rxjs doc on Github is a good reference for all the operators and components of Rx. Even if you’re not using Rxjs, it’s still useful – with the caveat that the names and signatures might be different on your platform.
Wrapping up
There’s a lot of useful information on Rx out there, and some great interactive demo/example tools like the Rxjs book and RxMarbles. All of these resources come at Rx from different angles, so it’s worth checking each of them out to see which works best for you.
Whichever version of Rx you’re learning, get an editor and a test console, or LinqPad, or the interactive examples in the Rxjs book, and try things out as you read them. Tweaking and trying things out along with a good resource is the best way to get your head into Rx.