What is Reactive Programming?
Reactive Programming is a way to build a scalable architecture that is resilient and
quick to react to stimuli.
Think of a spreadsheet. You have 3 cells A1, B1 and C1. You can configure
an equation against C1 as the SUM(A1, B1). Here Cell C1 can respond
to the
changes of A1
and B1 without any further actions from the user.
Here we can say this spreadsheet is reactive towards the changes.
The Cell C1 has been subscribed to the changes in cells A1 and B1, and does a simple
addition over the updated values and displays the same.
Isn’t this Cool? How can we use a similar strategy to programming? That’s reactive
programming: changes propagate throughout a system automatically.
In RP, Observables emit data and send it to the subscribers. In the above
example, Cells A1 & B1 are Observables, and Cell C1 is a subscriber.
Reactive representation of Spreadsheet Cells
Why Reactive Programming?
Reactive programming is about creating an architecture that supports:-
-
Elasticity: The system stays responsive under varying workload.
-
Message Driven: Communication between systems through messages.
-
Responsive: The system responds in a timely manner if at all possible.
-
Resilient: The system stays responsive in the face of failure.
Use Cases
Here are some of the examples where Reactive programming is used
-
Facebook Spark AR Studio uses reactive programming to create relationships between
objects, assets and values.
-
Linkedin is using reactive principles to build online indicators for users on its
social network.
-
Verizon Wireless had used reactive principles to reduce response time to half in
their eCommerce website.
Netflix is a big believer in the Rx model because Rx has made it much easier for us
to build complex asynchronous
-
programs. Netflix Falkor provides developers with a unified model for interacting
with both local and remote data, and it’s built on top of Rx.
When to use Reactive Programming?
Applications nowadays have an abundancy of real-time events of every kind that enable a
highly interactive experience to the user. We need tools for properly dealing with that,
and Reactive Programming is an answer.
-
Render a UI that combines data from multiple data sources
-
Create a realtime model of stock prices
-
Log/display data from data sources such as wind, temperature or pressure sensors
-
Highly Concurrent Message Consumers
-
Highly depended on connected systems: Whenever there are so many depended on
connected systems in which each of them are waiting for their successor to process
some data and use that result.
Evolution of Reactive Programming in Different languages
Rx*library family is widely available for many languages
and platforms
Let's look at Code Now
StockServer
Stock Server provides a stream of Stock
info via the method getfeed .
Observable
Notice the Observable , Observable is simply a collection
of data that waits to be invoked (subscribed) before it can emit any data.
Main
Here we are subscribing to the stocks feed on StockServer
and prints the values and errors.
Subscription
Demo Repo
References