You may have caught wind of this programming paradigm and wondered what all the fuss is about. For one, it’s by no means a new fad…well, not entirely. The techniques of Reactive Programming have existed for several years and have only become increasingly relevant with the nature of modern applications. If you’re working on a project (web application or mobile app) that makes use of some kind of real time components, then knowing RP techniques can be a huge benefit to your project.
There are a number of wordy ways to define it, but put simply, Reactive Programming is programming with asynchronous data streams.
Reactive Programming = Programming with asynchronous data streams
What exactly is a data stream?
A data stream is a sequence of events ordered in time, and can emit three different types of events: a value of some type, an error (if there is one) and a complete signal.
Some streams don’t complete, for example a stream from an event like the onkeypress or onclick. However, if you have a stream from a third party API returning some JSON content, that’s definitely going to complete.
These streams and these events can also be subscribed to and unsubscribed to. So whenever it’s updated, the subscriber will be notified and react accordingly.
Let’s look at two examples that might better portray this idea.
Take the following expression:
z = x + y
This means that ‘z’ is assigned to the result of ‘x’ and ‘y’.
In Reactive Programming, the value of ‘z’ will be updated according to the values of ‘x’ and ‘y’. So the program will watch for any changes in ‘x’ and ‘y’ and then ‘z’ will react to it.
Another expression of Reactive Programming can be found in good old spreadsheets. Remember working with functions like SUM that captured the total from values in a range of cells? That is essentially how RP works. The spreadsheet has a cell that is dependent on other cells. Let’s say you want the sum of 7 cells, and make use of the SUM function (or something of the like) to get the total, changing the value of any of the cells will cause the cell with the sum to react to that change.
Reactive programming focuses on triggering events and as an event happens there are certain aspects of the program that will react to it.
Observables & Observers
An observable is basically the stream being observed, it emits the value, error and the completed signal. The observer is what subscribes to that observable and reacts to whatever item or sequence of items that the observable emits. It facilitates concurrent operations because it doesn’t block while waiting for the observer to emit objects, it all happens asynchronously. What’s the benefit you ask? You don’t have to wait on dependent tasks to complete, they can just all fire at the same time.
Okay, so how do we actually do all this stuff?
The answer = Reactive Extensions (or ReactiveX).
ReactiveX is a library for composing asynchronous programs by using observable sequences. It provides a long list of operators which allow us to filter, select, transform, combine and compose observables. We can also map data from one stream to another or use a stream as an input for another.
ReactiveX is available for several programming languages, some of which I’ve listed below:
Reactive Programming can be a bit difficult to learn and in some cases requires a slightly different way of thinking. So don’t stress too much if you can’t wrap your head around it the first time around, but once you do, it can prove to be a great tool in your arsenal when it comes to developing with the best possible approach to your project.
We’d love to innovate with you.
Business Software. Sorted™