Reactive Programming: Why should you care?

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.

Reactive UI in Spreadsheet

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.

Reactive programming is an asynchronous programming paradigm concerned with data streams and the propagation of change.

https://en.wikipedia.org/wiki/Reactive_programming

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.

  • Retrieve data from a database and filter out data based on user settings/configurations.
  • 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<StockInfo> , 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

Download demo code from https://github.com/sujithps/stock-exchange.

References

Reactive Manifesto: https://www.reactivemanifesto.org/

Reactive UI: https://www.reactiveui.net/docs/

https://gist.github.com/staltz/868e7e9bc2a7b8c1f754

https://blog.danlew.net/2017/07/27/an-introduction-to-functional-reactive-programming/

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
SUJITH SASIDHARAN

SUJITH SASIDHARAN

Tech enthusiast ⚙️| Fun lover 😎| Programmer 🧑🏽‍💻 | The only time I can’t tell a joke is when someone asks me to tell one 😇 | www.kiebot.com