Reactive programming is an asynchronous programming paradigm concerned with data streams and the propagation of change.
That is the definition you would find on a technical document while defining this term. But what does it exactly mean? And why is there an urgent need to address our understanding of Reactive Programming?
This blog is an attempt to answer any questions you may have on this topic. Of course, if you have any other questions, feel free to get in touch with us and we will try to answer them as well.
Can you define Reactive Programming in clear English?
The definition above can be clearly explained as follows: Any change in the value of a field, variable, error etc. should be notified to other objects that depend on this value. Further, these objects should react according to the new value.
What is the importance of Asynchronous work?
User experience depends on a large volume of complex calculations that need to be completed in the background to ensure that the application is more responsive and the user has a smooth and easy flowing experience.
Can you explain Reactive Programming with a simple example?
Let’s consider an example expression C= A + B, where C is dependent on value of A & B. Whenever value of A and B changes it should change the value of C without having to re-execute the expression C = A + B.
In the same expression, in case the value of B is also dependent on A, any updates in the value of A should be completed before it updates the values of B and C in that order. This sequence of updating values continuously over a period of time is where Reactive programming is at its magical best.
Reactive program enables any changes in the model to be reflected in the corresponding view automatically and vice versa.
Reactive Programming is programming with asynchronous data streams.
Reactive Programming is the programming model that comes out in response to the Reactive Manifesto
When using reactive programming, data streams are going to be the spine of your application. Events, messages, calls, and even Errors are going to be conveyed by a data stream. With reactive programming, you observe these streams and react when a value is emitted.
4 Ways to identify Reactive Systems
Reactive programming vs Reactive system
Using reactive programming does not build a reactive system. Reactive systems, as defined in the reactive manifesto, are an architectural style to build responsive distributed systems.
A reactive system is an architectural style that allows multiple individual applications to work together as a single unit, providing good response time, and flexible enough to individually scale up/down, load balancing etc.
Four Characteristic properties of Reactive Systems are:
How do you create Reactive applications?
Answer is Reactive Extensions (ReactiveX) (An API for asynchronous programming with observable streams)
It is an implementation of the reactive programming principles to “compose asynchronous and event-based programs by using observable sequence”. With RX, your code creates and subscribes to data streams named Observables.
ReactiveX is a combination of Observer pattern, the Iterator pattern, and functional programming
RX = OBSERVABLE + OBSERVER + SCHEDULERS
Let’s understand these points in little more detail,
Observable: Observable are nothing but the data streams. Observable packs the data that can be passed around from one thread to another thread. They basically emit the data periodically or only once in their life cycle based on their configurations. There are various operators that can help observer to emit some specific data based on certain events, you can think observers as suppliers. They process and supply the data to other components.
Observers: Observers consumes the data stream emitted by the observable. Observers subscribe to the observable using subscribeOn() method to receive the data emitted by the observable. Whenever the observable emits the data all the registered observer receives the data in onNext() callback. Here they can perform various operations like parsing the JSON response or updating the UI. If there is an error thrown from observable, the observer will receive it in onError().
Schedulers: Remember that Rx is for asynchronous programming and we need a thread management. This is where schedules come into the picture. Schedulers are the component in Rx that tells observable and observers, on which thread they should run. You can use observeOn() method to tell observers, on which thread you should observe. Also, you can use scheduleOn() to tell the observable, on which thread you should run. There are main default threads are provided in RxJava like Schedulers.newThread() will create new background that. Schedulers.io() will execute the code on IO thread.
RX provides toolbox to implement Reactive Programming concepts by combining the observer and iterator patterns and functional idioms. It provides multiple functions to combine, merge, filter, transform and create the data streams.
Reactive Programming has got quite adoption now and there are several frameworks, libraries, tools are in race to achieve Reactive Manifesto.
Flavors of Rx:
Rx.NET: The Reactive Extensions for .NET is a library for composing asynchronous and event-based programs using observable sequences and LINQ-style query operators.
RxJS : The Reactive Extensions for JavaScript (RxJS) is a library for composing asynchronous and event-based programs using observable sequences and LINQ-style query operators in JavaScript which can target both the browser and Node.js.
RxJava: Reactive Extensions for the JVM – a library for composing asynchronous and event-based programs using observable sequences for the Java VM.
RxScala: Reactive Extensions for Scala – a library for composing asynchronous and event-based programs using observable sequences
RxCpp: The Reactive Extensions for Native (RxCpp) is a library for composing asynchronous and event-based programs using observable sequences and LINQ-style query operators in both C and C++.
Rx.rb: A prototype implementation of Reactive Extensions for Ruby (Rx.rb).
RxPy: The Reactive Extensions for Python 3 (Rx.Py) is a set of libraries to compose asynchronous and event-based programs using observable collections and LINQ-style query operators in Python 3.
Though Reactive Programming is relatively new, as applications are becoming more complex day by day with different target devices working with asynchronous calls is more common and hence Reactive Programming is required in Real world projects.