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.

Advertisement
Advertisement - Article continues below

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.

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.

Advertisement
Advertisement - Article continues below

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.

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

The IT Pro guide to Windows 10 migration

Everything you need to know for a successful transition

Download now

Managing security risk and compliance in a challenging landscape

How key technology partners grow with your organisation

Download now

Software-defined storage for dummies

Control storage costs, eliminate storage bottlenecks and solve storage management challenges

Download now

6 best practices for escaping ransomware

A complete guide to tackling ransomware attacks

Download now
Advertisement

Most Popular

Visit/security/identity-and-access-management-iam/354289/44-million-microsoft-customers-found-using
identity and access management (IAM)

44 million Microsoft customers found using compromised passwords

6 Dec 2019
Visit/cloud/microsoft-azure/354230/microsoft-not-amazon-is-going-to-win-the-cloud-wars
Microsoft Azure

Microsoft, not Amazon, is going to win the cloud wars

30 Nov 2019
Visit/hardware/354237/five-signs-that-its-time-to-retire-it-kit
Sponsored

Five signs that it’s time to retire IT kit

29 Nov 2019
Visit/business/business-strategy/354195/where-modernisation-and-sustainability-meet-a-tale-of-two
Sponsored

Where modernisation and sustainability meet: A tale of two benefits

25 Nov 2019