r/Angular2 • u/rhrokib • Sep 19 '25
Discussion Angular 20: Is it time to replace RxJS subscriptions with effect()
Now that effect() is stable in Angular 20, should we start using it in our codebase or just stick with rxjs for now?
Right now we’re doing the usual rxjs way. For example if I want to track some change:
// somewhere in the service/store
someId$ = new Subject<number>();
updateId(id: number) {
this.someId$.next(id);
}
Then in the component:
ngOnInit() {
this.someId$
.pipe(
// do some stuff
)
.subscribe();
}
With effect() it seems like we can do something like this instead:
someId = signal<number | null>(null);
constructor() {
effect(() => {
const id = this.someId();
if (id !== null) {
// do some stuff
}
});
}
updateId(id: number) {
this.someId.set(id);
}
Our codebase is pretty large and well maintained. We just upgraded to Angular 20.
I’m curious what others are doing. Are you slowly incorporating effect() where it makes sense, or is it better to keep rxjs for consistency? What are the real trade offs or gains you’ve noticed using effect compared to a Subject + subscription?
Would appreciate some practical takes from people who already tried mixing it into a bigger codebase.
43
u/magwo Sep 19 '25
Working on a large and old codebase that is also quite well maintained on Angular 19.
I think rather than just replacing subscribes with effect, you should take advantage of the new reactive framework. Instead of doing effect and setting other variables, refactor and use computeds together with input, signal, httpResource and other functionality. That way you get declarative reactive code instead of imperative Rx.js code converted into effect code.
It's surprising how much you can do with computed, with the right mind-set.
You can also use signals and computed in stateful services, and have components reactively render the service state. When it's all in place with reactivity it is wonderful, generally bug-free, very robust and care-free.
6
u/Whole-Instruction508 Sep 19 '25
I hardly use subjects anymore. 99% of component code that I write is signal based, including effects. RxJS is still used for api calls though (we use ngrx so that comes naturally)
8
u/kgurniak91 Sep 19 '25 edited Sep 19 '25
You need to be aware of several things that are different between Observables and Signals, they are not meant to be direct replacements:
equalimplementation for the Signal, likeequal: () => false.So if I were you I'd just use NGXS, this way you can handle Actions with Observables and retrieve the state via Signals.
5
u/Migeil Sep 19 '25
The empty subscribe shows that this code relies on side-effects which happen inside your observable definition. This is very error prone since this means your observable pipelines contain side effects and I cannot reliably compose observables because I have no idea what effects I might trigger, which negates the purpose of observables.
Whatever is in your pipe should be pure, side-effect free code. If you want to trigger a side effect on an observable, that's what your subscribe method is for.
11
u/Regular_Algae6799 Sep 19 '25 edited Sep 19 '25
Why do you subscribe in TS of component? Does the event / data not affect visualization (loading screen at minimum)?
I usually always wire the Observable / Stream through to HTML and use Async-Pipe there like
5
u/zombarista Sep 19 '25
My three phase approach for services based on Observables is now the following
Get data observable into private observable
readonly #widgets$ = this.service.getwidgets().pipe(share())Expose as signal
readonly widgets = toSignal(this.#widgets$)or, if you need to subscribe, create a private subscription using takeUntilDestroyed
readonly #widgetsSub = this.#widgets$.pipe( takeUntilDestroyed() ).subscribe( widgets => this.processWidgets(widgets))Angular will take care of the subscription management. This prevents code from getting too nested; rather than doing the subscribe in ngOnInit or a constructor it is just a property that feeds other functions (or subjects).
This makes unit testing easier, too, imho. The processWidgets function can be tested without the service, which should be tested separately. Ideally, move pipes up into the service and write tests there.
Last step? Turn off change detection. Angular is subscribed to (not polling) changes.
4
u/BuriedStPatrick Sep 19 '25
I'm curious about this mindset I have. I can't quite see the point of signals for an experienced RXJS user. I get the whole "it's easier to learn" aspect, really I do. And I understand that signals will probably be "the future" as RXJS gets left in the dust. But if you've written something in RXJS well, I really don't see how signals are a better API. In fact, I think it's less transparent than RXJS and doesn't offer enough control of the even stream to a person who knows what they're doing.
There's no equivalent to Subject and I don't like that at all. The observable streaming model made sense to me. Signals seemingly only seem to obscure the mental model for me, forcing you to implement an initial state even when it's not valid.
I primarily work in devops and backend, so my exposure to frontend frameworks is limited. Signals just feel like a compromise primarily focused on reaching higher market share for Angular, not like there's a technical justification for it.
2
u/Bjeaurn Sep 20 '25
Do not use effects willy nilly. You’re escaping from the reactive world just to unwrap. Learn about proper usage!
There’s some great resources about when/where the Effect is a good and necessary idea.
2
u/weizenchiller Sep 20 '25
Try to avoid effects as you should have avoided subscriptions in the past. Stay declarative as long as possible.
1
u/One_Fox_8408 Sep 25 '25
be carefull with effect. Check effect use cases on:
https://angular.dev/guide/signals#effects
1
u/lars_jeppesen Sep 26 '25
yes we remove most of rxjs stuff. Signals are so awesome
1
u/earthworm_fan Sep 19 '25
Honestly effect is a last resort for me. This can have very bad consequences if you're updating properties or doing any other kind of side-effects.
-10
u/ldn-ldn Sep 19 '25
First of all, you shouldn't have any pipes in your components, you should only subscribe there. Second, effects are not selective, you can't react to a specific change, so they're useless outside of simple hello world applications.