Introduction to Reactive programming: part 1
Hi there! How are you? I hope you are well!
Recently, I am looking for better ways to interact with APIs, handle I/O and general async operations inside an application. I came across the concept of Reactive programming while I was trying to understand how RxJs works! Of course these two words refer to completely different things. This is the goal of this article, to understand better which are the differences between them and clarify things in order to dive deeper in future posts!
Reactive programming
This term refers to a programming paradigm! We cannot argue enough at this point so our next question is:
What is a programming paradigm?
A programming paradigm is the style in which well-defined features of a programming language can be used to create programs. For example, when you use objects and the relations between them in a certain way in order to write your application or program, then we say that you are using the Object-oriented programming paradigm. The meaning of a paradigm is explicitly expressed on the actual implementation of your program using the language arsenal of tools. In the same example, you can use OOP or not! The paradigm is not part of the language itself but more like a subset of features and a way you can use those features to structure your code. In JavaScript everything is an object and you can use the OOP paradigm, or even better a subcategory of it particularly named Prototype-based OOP and that is because the typical OOP is Class-based! But, with JS you can write programs with the Functional programming paradigm if you use certain patterns or methodologies to write your program, like function composition! Or even using the Imperative programming paradigm!
So, going back to our original question about what Reactive programming is we can say it is a different programming paradigm than OOP or Imperative programming or even Functional programming! Ok so far, but the next question is this:
What is the mental shift for this paradigm?
The base concept of Reactive programming is the data stream (or Observable in RxJs)! We can say that a data stream is the core type of this paradigm, analogous to object for OOP and function for Functional programming! But what is a data stream?
Data streams
According to Wikipedia, a stream is a sequence of data elements made available over time! And this is the huge difference in the mental model! In other paradigms, we think of data as single values or chunks. In Reactive programming there is a continuous flow of data like a water stream! We think of data as collections of elements over time! This introduces us to another notion. How do we communicate the data flow changes through our system? With the help of the Push protocol! Oh dear!
Push systems
A Push system is similar to the Observer OOP pattern! What is the Observer pattern, right?
I think this is a good place to take a break and continue to the next post in what seems like a series of posts eventually!
So, until next time…
Cheers!