RxJS6 asObservable() needed on a Subject?

RxJS6 asObservable() needed on a Subject?

When is asObservable() needed on a Subject (e.g. BehaviorSubject) to get a observable of the subject? The subject isself can be casted to an Observable as well.

Questions
  1. What are the technical differences between name1$ and name2$?
  2. Which one should be used (name1$ or name2$)?
Code Sample
import { Observable } from 'rxjs/Observable';
import { BehaviorSubject } from 'rxjs';

export class Person {
  private nameSubject: BehaviorSubject<string> = new BehaviorSubject<string>('lorem');

  public get name1$(): Observable<string> {
    return this.nameSubject.asObservable();
  }

  public get name2$(): Observable<string> {
    return this.nameSubject;
  }

  public setName(value: string): void {
    this.nameSubject.next(value);
  }
}

Thank You for your answers in advance!


In general, it's recommended to use asObservable() only when using RxJS in JavaScript. With TypeScript you can use just type casting and it won't let you call next() and so on.

private nameSubject: BehaviorSubject<string> = new BehaviorSubject<string>('lorem');
public nameSubject$: Observable<string> = this.nameSubject;

This is the officially recommended way of turning Subjects to Observables in TypeScript. For details see https://github.com/ReactiveX/rxjs/pull/2408#issuecomment-282077506.

RxJs Subjects: A tutorial. If you have some experience with…, asObservable().subscribe((data) => { console.log("First subscriber got data >>>> > "+ data); }); subject.next("Second value") // Console result: Example. The following is an Observable that pushes the values 1, 2, 3 immediately (synchronously) when subscribed, and the value 4 after one second has passed since the subscribe call, then completes:


There is not much difference, with asObservable() it mainly just block your access to observer methods - complete, next , error to prevent you from accidentally calling them

const a = new Subject();
console.log(a.asObservable().next)
// undefined

it is better to return asObservable() to be on the safe side if you not using that particular stream as observer

Subject, Every Subject is an Observer. It is an object with the methods next(v) , error(e) , and complete() . To feed a new value to the Subject, just call next(theValue) , and � So yes, if you have 100% confidence in the type safety of your entire project and the capabilities of your team, you can just cast the Subject to Observable. But if you don't, .asObservable() provides a bit more safety that no one consuming your API is sneakily nexting values into your Subjects.


asObservable() creates a new Observable, which wraps the Subject, so the wrapped observable doesn't have next() method, as well as complete() and error(). I think it makes more sense in JavaScript, where returned object isn't restricted by it's type. But in TypeScript you can hide these methods just by narrowing the type, it should be enough in most cases. You just tell that you return an Observable, and a user of the API doesn't know that it's a Subject, of cause it can cast it back to Subject, but it is not how the API should be used.

See also: When to use asObservable() in rxjs?

asObservable, asObservable(). Hides the identity of an observable sequence. Returns. ( Observable ): An observable sequence that hides the identity� Subjects are multicast. The second super power of subjects is that they support multiple subscriptions.In other words, they are multicast. Subjects are like EventEmitters: They maintain a registry of many listeners.


RxJS Subjects, Subject: is the equivalent to an EventEmitter, and the only way of multicasting a Code that calls iterator.next() is the Consumer, "pulling" out multiple values from the iterator (the Producer). Do you need to find an operator for your problem? We need to create the Notification Observable directly in the function passed to the retryWhen operator. This function takes as input argument an Errors Observable, that emits as values the errors of the input Observable.


On The Subject Of Subjects (in RxJS) | by Ben Lesh, return () => button.removeEventListener('click', handler); });. Why show this when it has nothing to do with Subjects? Well, for one thing� Different approaches to managing RxJS subscriptions in Angular applications lead to different trade-offs in terms of verbosity, robustness or simplicity. Let’s explore our options together!


BehaviorSubject, RxJS v6+. import { BehaviorSubject } from 'rxjs';. . const subject = new BehaviorSubject(123);. . // two new subscribers will get initial value => output: 123, 123. Subjects. Recipes. Concepts. Powered by GitBook. share. signature: share(): Observable. Share source among multiple subscribers. 💡 share is like multicast with a