I know you are tired and exhausted from searching for a good introduction about reactive programming, you listened for a lot of people but finally, you find this series.
In this series, I'm going to give you the bedrock of reactive programming,
let's dive together and learn what we need.
There are plenty of bad explanations and definitions about reactive programming, so let's cut it in a little definition.
is programming with asynchronous data streams.
come down, this isn't anything new, event buses or typical click events are really an asynchronous event stream which you can observe and do some side effects, so what is the reactive programming main idea? reactive programming gives you the ability to create data streams of anything.
Streams: are cheap and ubiquitous, anything can be a stream, variables, user inputs, properties, caches, data structure, etc..
a stream is a sequence of ongoing events ordered in time,
it can emit 3 different things:
- value (of some type)
- error (something wrong happened)
- completed signal (when the stream is done or completed) we captured these emitted events only asynchronously, by defining a function that will execute when a value is emitted, another function when an error is emitted and another function when completed is emitted, sometimes we can omit the last 2 functions and we can just focus on defining the function for values.
The listening to the stream is called subscribing. by subscribing to an
observable you are going listen to any new change
The functions that we are defining to deal with the ongoing events are called observers, it contains 3 functions (next, error, complete).
It is an object usually given to the observable.subscribe(observer), Observable will call the Observer's next(value) method to provide notifications, a well-behaved Observable will call an Observer's complete() method exactly once or the Observer's error(err) method exactly once, as the last notification delivered.
Observable or subject is what is observed by observers.
we can imagine it as a data store that will send a notification whenever change happened to its value.
Oh that's the Observer Design Pattern, so please check it
There is no big difference, both are promising us with a value in the future when some event happens, but the main difference between them that Promise will emit a single value, on the other side Observable returned many values over time.
Reactive programming raises the level of abstraction of our code, so we can focus on the interdependence of events that define the business logic, rather than having to constantly fiddle with a large amount of implementation details. Code in Reactive Programming will likely be more concise
The benefits are more evident in modern web apps and mobile apps that are highly interactive with a multitude of UI related to data events, for example, "likes" on Facebook can be reflected in realtime to other connected users.
Reactive programming is really suitable for real-time web apps and mobile applications, it is really helpful when we are dealing with multiple components dealing with the same data source and interested in data change.
- The introduction to Reactive Programming you've been missing
- Observer pattern