ReactiveX library is present in many languages.
For Example,
- Javascript - RxJs
- Java - RxJava
- Go - RxGo
- Dart -RxDart etc .
- RxJS is a library for reactive programming using Observables, to make it easier to compose asynchronous or callback-based code.
- RXJS is used when we have requirement to work with streams of data instead of single values.
- RXJS has many operators to manipulate and transform the set of data received.
- stream of data - Supposed a click event is written on a button , there is a chance user might click on the button n number of times which emits data which is called stream of data.
- single Values - we are making an API call which returns the data or sends a failure message which is called single value like once the API gives the response its is completed.
npm Command to Install Rxjs
npm install Rxjs
Observables
Observables are wrapper around streams of data which emits data asynchronously.
Data streams can be created from many things
- UI Events
- Http requests
- File Sytems
- Array like Objects
- Memory/Cache

- Observables are used to watch these streams and emit functions when a value, error or complete signal is returned.
- Observables can be subscribed to by an Observer.
- Observer is there to execute some piece of code whenever observable sends any value, emits error or Observable reports that it is done
- Subscription means the subscribe method using which we tell the Observable that someone(Observer) is waiting for the values , errors from the stream of data.
- After receiving data Observer implements 3 methods using subscribe function
- next ( )
- error ( )
- complete ( )
- The next method will be called whenever a value is returned from the Observable.
- The error method is called whenever an error is returned from the Observable.
- The complete method is called whenever an observable is done i.e no more values will be emitted in the future from the observable.
- Some of the Observables might never finish as well.(Ex. When an observable is created for button click it never ends as user can click any point of time the button at any co-ordinate )
- We can pass 3 methods to the subscribe function or an object containing 3 functions.
- 1..subscribe(
- (value) => {}, (error) => {}, (complete) => {})
- 2. Var ob = {
- next: function() {},
- error: function() {},
- complete: function() {}
- }.subscribe(ob);
How does Observer know which method( next, error or complete ) to execute when receiving value from Observable ?
There is a contract between Observer and Observable using which observer is able to identify and call the appropriate methods.
Note
For Creating an observable we use next , complete and error methods



Join the conversation! Your thoughts help the community grow.