Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is observable, observer and subscribe in angular?

I am learning angular and i got confuse in these observable, observer and subscribe thing. So please explain.

like image 975
Amit Sharma Avatar asked Jul 25 '18 13:07

Amit Sharma


People also ask

What is subscriber and observable in Angular?

Observables in Angular are generally declarative i.e., if you ought to define a function for value publication. It won't be executed until and unless it is subscribed. The subscriber is termed as a consumer who receives the notifications until the function completes itself or until they manually unsubscribe.

What are observables and subscribe?

Observables are data source wrappers and then the observer executes some instructions when there is a new value or a change in data values. The Observable is connected to the observer who does the execution through subscription, with a subscribe method the observer connects to the observable to execute a code block.

What is difference between observable and observer?

Observer : Any object that wishes to be notified when the state of another object changes. Observable : Any object whose state may be of interest, and in whom another object may register an interest.

What is subscribe () in Angular?

Normally Subscription means an arrangement to receive something. Similarly, in Angular applications Observables will be connected to observers and whenever they observe a new value or change in data, they will execute code with the help of Subscription and all the subscribed components will receive the updated outcome.


3 Answers

Here is a simple visual to see the difference:

enter image description here

As seen above ... an Observable is a stream of events or data. They are often returned from Angular methods, such as the http.get and the myinputBox.valueChanges.

Subscribing "kicks off" the observable stream. Without a subscribe (or an async pipe) the stream won't start emitting values. It's similar to subscribing to a newspaper or magazine ... you won't start getting them until you subscribe.

The subscribe method takes in an observer. An observer has three methods:

  • The method to process each time an item is emitted from the observable.

  • The method to process any error that occurs.

  • The method to clean up anything when the observer completes. This last one is seldom used when working with Angular's observables.

Hope this helps.

(And I agree ... trying to see the forest through the trees of the docs is quite a challenge. I understand they are working to improve them.)

like image 194
DeborahK Avatar answered Oct 12 '22 22:10

DeborahK


Trying to explain with a really simple example:-

  1. Observable is like a youtube channel of someone else. (( It uploads new videos(data) from time to time, so it is a data source for you))

  2. Your youtube account is an Observer

  3. Your youtube account (Observer) can only get notifications about whether someone else's youtube channel (Observable) has uploaded a new video (has new data) or made a livestream (new event) only if you have subscribed to that channel

(Observer subscribes Observable to listen for new data/any event)

where observable is a data source, subscribe is like a method/function , Observer is generally on your side

like image 37
Aman Chawla Avatar answered Oct 13 '22 00:10

Aman Chawla


An Observable can be thought of as various data sources(ex: (userInputs)Events, HttpRequests etc).

here creating our custom observable.

    var observable = Observable.create((observer: any)=>{
       observer.next("Hii")
       observer.next("how are you")
       observer.complete()
    observer.next("This will not send to observer")
});
  • next() used to emit values to observer
  • complete() indicates that completion of observable is notified.

An Observer is basically who subscribes to Observable.

observable.subscribe(
    (data: any) => console.log(data), // for handling data
    (error: any) => console.log(error), // for handling error
    () => console.log('completed'); // for handling completion

);

Here you can learn more about Observable http://reactivex.io/documentation/observable.html

like image 28
Siva Sanjay Acchana Avatar answered Oct 12 '22 22:10

Siva Sanjay Acchana