What is reactive programming?

A look at the tools helping to build those apps reliant on asynchronous data streams

keyboard

Getting the right data to the right part of an application at the right time can be a challenge for many developers. If those streams of data don't come at the right time, all hell could break loose (or the app just stops working for a bit). Reactive programming is a method for overcoming such problems.

Reactive programming is a technique used to create applications that can cope with asynchronous data streams. Developing apps in this way can improve the user experience.

Such techniques can make an application seem more responsive. The application doesn't free when waiting for a data stream to come online.

Events, messages, calls, and even failures, can be turned into streams of data. An application using reactive programming will see these streams and react to them using its means to filter, create, transform, and connect any of those streams.

The code within a reactive application creates data streams of anything and from anything, such as HTTP requests, messages, notifications, changes to variables, cache events, sensor measures, and clicks. When this happens, the application becomes intrinsically asynchronous.

Reactive programming can turn virtually anything to a data stream to manage in an asynchronous manner. It also makes that job of handling errors simpler and makes code more flexible, readable, supportable, and simple to write.

The four principles of reactive programming

There are four principles to adhere to when creating reactive applications.

First, a responsive application is the main aim. It should be fast in reacting to all users.

Second, it should be resilient. This means it should apply suitable design and architecture principles in order to ensure responsiveness in all conditions.

Advertisement
Advertisement - Article continues below
Advertisement - Article continues below

Third, it should be scalable. I.e.  it should be easy to upgrade on demand to guarantee responsiveness under numerous load conditions.

Lastly, the foundation of a scalable, resilient, and responsive system is its message-driven architecture. A message-driven application may be event-driven, actor-based, or both.

Reactive Extensions

Reactive programming as a concept has been around for over 20 years but didn't take off until the introduction of Reactive Extensions. Reactive Extensions (also known as ReactiveX) are APIs or asynchronous programming with observable streams. This is how reactive programming works.

ReactiveX has two classes; observable and observer classes. An observable class is the data source of streams or events and an observer class is the one that uses (or reacts to) the produced elements.

An observable can have multiple observers so each data item produced will be received by each observer. In ReactiveX an observer subscribes to an observable. An observable then produces streams of data which an observer listens and reacts to. This sets off a series of operations on a data stream.

Advertisement - Article continues below

A subject is an observable extension that also implements an observer interface. This means that subjects can act as both observers and observables.

We can also apply operators to a stream. These define how and when the observables should produce streams. Most operators execute functions on an observable and return an observable.

Featured Resources

What you need to know about migrating to SAP S/4HANA

Factors to assess how and when to begin migration

Download now

Your enterprise cloud solutions guide

Infrastructure designed to meet your company's IT needs for next-generation cloud applications

Download now

Testing for compliance just became easier

How you can use technology to ensure compliance in your organisation

Download now

Best practices for implementing security awareness training

How to develop a security awareness programme that will actually change behaviour

Download now
Advertisement

Most Popular

Visit/policy-legislation/data-governance/354496/brexit-security-talks-under-threat-after-uk-accused-of
data governance

Brexit security talks under threat after UK accused of illegally copying Schengen data

10 Jan 2020
Visit/web-browser/30394/what-is-http-error-503-and-how-do-you-fix-it
web browser

What is HTTP error 503 and how do you fix it?

7 Jan 2020
Visit/policy-legislation/data-protection/354492/currys-pc-world-parent-firm-hit-with-ps500k-fine-over
data protection

Currys PC World parent firm hit with £500k fine over historic data breach

9 Jan 2020
Visit/security/ransomware/354483/travelex-disruption-caused-by-devastating-ransomware-attack
ransomware

Travelex disruption caused by devastating ransomware attack

8 Jan 2020