Reactive Programming

Antonio Canabrava
Loft
Published in
2 min readSep 30, 2019
An observer observing the data stream that is the view
Photo by Maarten van den Heuvel on Unsplash

You might be asking yourself, what is Reactive Programming and why is it important? There are lots of definitions out there, Wikipedia's, StackOverflow's, Microsoft's and the one from the Reactive Manifesto.

But the simpler ones are usually better. One of the best definitions (IMHO) is the one from @staltz.

Reactive programming is a development model structured around data streams.

And from that definition it is imperative that we differentiate Reactive Programming from RX (Reactive eXtension), whilst the former refers to the development model, the latter refers to the implementations of the reactive programming principles.

Where do we use it here at Loft?

In one of our previous posts, we presented NestJs and why we use it here at Loft. But how does NestJs connect to Reactive Programming? Through Interceptors.

Interceptor is a class of components that allow you to extend a function's capabilities. You could, for instance, bind logic before/after a method execution or transform the result/exception returned by that function.

At Loft, we have two main use cases for interceptors, intercept UTM information and as a function logger. In the former, we capture information to check from which campaign a specific user arrived to a specific endpoint. The latter is a simple way of profiling specific functions, allowing us to check the duration of its execution.

Why use it?

Here are some key points about why you should start thinking reactive and using it as well.

You can listen to and combine data from different sources and formats, such as a call to a relational database and a non-relational database.

If you need to call the same promise returning function lots of times, a single definition of stream might be a good option.

Observables have some advantages over promises besides being able to process a data stream, it can be canceled or retried.

Reactive programming is not a silver bullet to working with asynchronous data. It also comes with steep learning curve, since you will have to start thinking in "reactive way" as opposed to the imperative way. But knowing how and when to use is imperative as knowing any other design pattern.

Finally, follow the link to our open positions and join us! https://jobs.lever.co/loft

--

--