liveBook · Manning

Subscription

As explained in Modern Angular

An RxJS subscription is an object representing an active connection to an observable and the execution associated with receiving its notifications.

A subscription represents an active connection to an observable and the execution associated with receiving its notifications. Losing the observable reference does not stop that execution: a subscription can become a “zombie” subscription, continuing work after a page or component no longer needs it. section 5.3.1

Manage subscriptions through either the subscription object or the observable pipeline:

  • Store the object returned by subscribe and call unsubscribe() to terminate that particular subscription. section 5.3.2
  • For multiple component subscriptions, prefer a lifecycle-aware pipeline such as takeUntil with a destroy$ Subject, triggered from ngOnDestroy. section 5.3.2
  • Treat cleanup as a user-experience and resource concern: routed components that retain subscriptions can accumulate memory leaks or continue connections unnecessarily. section 5.3.1

Stopping an interval

const subscription = interval(1_000).subscribe(console.log); subscription.unsubscribe(); The explicit handle is simple, but a component with many subscriptions can become cluttered; a shared lifecycle signal such as takeUntil(this.destroy$) centralizes termination. section 5.3.2

let obs$ = interval(1_000);
obs$.subscribe(console.log);
obs$ = null;

Inspect the minimal zombie-subscription example: assigning null to the observable reference does not terminate the active interval subscription. section 5.3.1

Signal interop does not eliminate lifecycle concerns. toSignal subscribes to its observable, so a service-backed socket may remain open after consuming components disappear. Explicit connection and cleanup design can keep such shared resources bounded. section 7.3.3

How Modern Angular covers it

How deep it goes
Defines the returned object, demonstrates unsubscribe and takeUntil, and stops short of a complete shared-connection disposal strategy.
Examples
Uses interval logging, explicit unsubscribe, destroy$ cleanup, routed query parameters, and a notification web-socket service.
What it assumes
Assumes familiarity with observables, subscribe, Angular components, and lifecycle hooks while introducing subscription cleanup problems concretely.
What you can do after
Operate subscription lifecycles by retaining handles, applying pipeline termination, and recognizing resource leaks in signal interop.
How it connects
Connects observables with subscribe, unsubscribe, takeUntil, Subject, ngOnDestroy, Angular routing, signals, and web sockets.

Also explained in

Compare how 2 books explain Subscription →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern Angular. The section links point to the parts of the book it draws on.