RxJS Subjects in Angular

The Subjects in RxJS are special observable which acts as both observer & observable.

RxJS Subjects in Angular
RxJS, Programming, thetechfoyer,thetechfoyer.com

Observer Pattern: Observables are a way that may be used for asynchronous programming and event management, in addition to handling numerous values that are passed out over a period of time.

The term "subject" refers to an object that keeps a list of its dependents, which are referred to as "observers," and automatically notifies them of any changes in the object's state. Although this style is not exactly the same as the publish/subscribe design pattern, it is highly comparable to that structure.


Subjects: Subjects make it possible for numerous observers to subscribe to them, and then they broadcast the value that they have received to those observers respectively. Those listeners are immediately notified if an event takes place, such as the arrival of a new value. 


Every Subject is an Observable. 
You can subscribe to a Subject by giving an Observer, and the Observer will begin to receive values in a typical fashion. The Observer is unable to determine whether the execution of the Observable is coming from a Subject or a plain unicast Observable because it is not aware of the source of the execution.
Within the Subject, the subscribe method does not initiate a new execution that produces values. This function merely adds the specified Observer to a list of Observers, functioning in a manner that is analogous to the way addListener is typically implemented in other libraries and languages.

Every Subject is an Observer. 
There are three methods that are associated with this object: next(v), error(e), and complete(). Call next(theValue) method in order to provide the Subject with a new value. This value will then be multicasted to all the Observers who have registered to listen to the Subject.


Observable in Angular
· Observables are a part of the RxJS (Reactive Extensions for JavaScript) library in Angular.
· Observables make it easy for your application's components to communicate with one another.
· Observables allow you to subscribe to receive notifications anytime new data or events are emitted, allowing you to react promptly to changes.
 
Subject in Angular
· A unique kind of Observable in RxJS, that enables values to be multicast to several Observers.
· Subjects are multicast, whereas simple Observables are unicast (each subscribing Observer owns a independent execution of the Observable).
· Subjects like Event Emitters, maintain a registry of many listeners.


Observable Vs Subject

Observable Subject
Are unicast, Each subscribed observer has its own execution of the observable. Are multicast. Maintain a list of observers, and notify all of them whever a new value emitted.

Are Declarative, represent a blueprint for a data strem, but do not emmit values until subscriber subscribe to them

Observer(s) >>> Observable

Are Imperative, they can emmit value independently.

Observable >>> Observer(s) 

Do not have initial value. Start emitting values only when subscriber is listing. Behavior subjec, have initial values. on subscription, it immediately emit the last value it received or the inital value if no value has been emitted yet to subscriber
Generally used, when you want to emit data in a lazy and declarative way, as handling HTTP request, user events or other async data soruce. Generally used, where you want to multi cast events or share data between multiple subscribers as inter-comonent communication or event broadcasting.



Methods Collection



Using Subjects in a Service is a good pattern for
sending data to multiple components,
receiving data from multiple components,
updating every component accordingly