The concept of reactive programming is currently thriving. It consists of dealing with asynchronous data flows called streams. One of the libraries to offer this concept is ReactiveX, which spans multiple programming languages and takes the best ideas from functional programming and the Observer and Iterator patterns.
The goal is to achieve an easily scalable responsive application.
Here are some of the advantages of reactive programming:
- You can avoid callbacks
- It makes asynchronous programming easier
- Simplifies work
- Readable code
- Numerous operators
Reactive systems based on reactive programming should be:
- Responsive: fulfill set response times.
- Resilient: remain responsive even in the event of a failure.
- Elastic: remain responsive even when there is a high workload.
- Message-driven: minimize coupling between components by establishing an asynchronous message exchange.
To give you an idea of the real power of reactive programming, I’m now going to show you a small example with RxJava2. In this example, you want to perform the following steps:
1. Obtain all the under-16-year-old students in a class.
2. Then get each student’s grades.
3. Combine these values.
4. Send this information to your API every 2 seconds
5. Run it in the background.
6. Once completed, show the information to the user.
As you can see, the readability and power afforded to us is huge.
Reactive programming can be implemented in numerous languages. Redux web developers sometimes recognize the need for asynchronous tasks. While libraries like Redux-Thunk or Redux-Saga do allow them, in this article we’ll focus on Redux-Observable, which takes advantage of the full power of reactive programming using RxJs.
Redux-Observable abstracts connecting to the Redux store and handles subscriptions so that you don’t have to worry about listening for actions or sending them yourself.
Redux-Observable does this through epics. These are functions that take a stream of actions in and return a stream of actions out or, put simply, they receive one action and return another.
Let’s take a look at an Epics example:
As mentioned above, Redux-observable handles all types of actions. In our example, thanks to ofType, we capture the type of action, then map our action and carry out the ajax request. This returns a response which we handle in the appropriate manner, in this case by calling the userLogin function.
Where does the true potential of reactive programming lie?
Reactive programming’s true potential lies in the use of operators. As you can see in our code sample, there’s a takeUntil operator that permits cancelling an operation if a particular action arrives. Added to this is a huge list of operators to add lots of functionality in very few lines of code. You can find the complete list here: https://www.learnrxjs.io/operators/.
This reactive concept originally emerged ten years ago, but I now consider it to be the best option to include in our developments as it allows you to deal with asynchrony and complexity more easily while harnessing the power of the ReactiveX library which provides support for many languages.
In Teldat we use Redux technology to develop certain sections of the Teldat SD-WAN solution.