What are Reactive Extensions (Rx)

As a developer it is important to keep up to date with new technologies as much as existing ones. Conferences such as Developer Developer Developer North are great for this, giving me the opportunity to explore technologies I may not be aware of, and this is where I first came across Reactive Extensions.

What are Reactive Extensions?

I’ll start with a quote from the reactive extension library on MSDN:

The Reactive Extensions (Rx) is a library for composing asynchronous and event-based programs using observable sequences and LINQ-style query operators. Using Rx, developers represent asynchronous data streams with Observablesquery asynchronous data streams using LINQ operators, and parameterize the concurrency in the asynchronous data streams using Schedulers. Simply put, Rx = Observables + LINQ + Schedulers.

To me, that’s not very helpful, and I know what Reactive Extensions do! So I’ll try and put it in clearer terms. Reactive Extensions allow you to subscribe to a stream of data. This could be many things, from mouse movement events, to tweets coming from twitter’s live streaming API. By “subscribe”, I mean you can define a function to be performed when the stream of data meet certain criteria.

The best way of explaining further is with a simple demo. My favourite to really show what Reactive Extensions are is a simple demonstration of using the mouse to draw, more specifically, creating the mousedrag event, and drawing something every time it occurs. I’m going to be using C# in my examples below, because I think it’s an easy language to be able to interpret, however it is important to emphasise that Rx is available in many languages, and I have also used it in JavaScript myself.

To do this, first we need to create our streams of data. We turn these into observables, which means they are streams of data we can subscribe to. In the below example “DrawingCanvas” is just the canvas element in my WPF app the events are coming from (and I will draw to).

The above is very simple. We use the Observable.FromEventPattern to convert an event into an observable. You need to tell it what type of event arguments to expect, however the rest should be fairly obvious.

Now we have our three streams of data. One is the stream of all mouseDown events, one all mouseUp events, and the third is all mouseMove events. The next bit is where Rx shows it’s usefulness. We can combine these events to form a new observable stream of data:

So what is this doing? Well it pretty much describes it in the code. We look at the mouseMove stream of data, and we ignore it until we receive a mouseDown event. Then we take all the mouseMove events until we receive a mouseUp event. The “Repeat” call at the end just tells it to repeat the process and not to stop after one single pattern. Very simply here we have managed to setup the stream of data for mouseMove events whilst the mouse button is held down.

I now have my stream of data, what do I do with it? This couldn’t really be simpler, I can just subscribe to it:

It is that simple. I’ve obviously setup a DrawLine method to do that work, and pass to it the the current position. To get the current position I can access the eventArgs through the EventArgs property on the EventPattern.

There is a further simplification I could make, and that is to change my observables to the following:

In this version I select only the positions for my streams, as that is the only event data I am interested in. I can then change my subscription to be simply:

As now I’m just receiving Points in my observables.

You can download the example app from here: Download

Why would I use it?

In my opinion, this is still a very good question. Reactive Extensions does not allow you to do anything new, it’s all available already, it just defines a new way to write your code. However this is powerful, and is not to be underestimated. The example above was very simple, and wouldn’t have been a lot more complicated if it was written using traditional methods. However you can imagine if you were to write an example using the twitter streaming API, the asynchronous nature of Rx, and the ability to use linq on streams of data could be very useful. Harnessed correctly, Rx could turn a potentially complicated section of code into a nice concise solution.

I have yet to use Rx in anger, so for me the jury is still out on it. However I like the syntax, and the promise of Rx, and when I spot an opportunity, I will certainly be considering Rx as a possible solution!

Further Reading

So Rx has been about for a few years, and is constantly evolving so keep that in mind, as some of the following links may have syntax that doesn’t work in the current version of Rx. I’ve found each of the following useful at some point however:

Leave a Reply