Unsubscribing in angular, The Right Way

Unsubscribing in angular, The Right Way

One of the things even developers who have been into Angular for a while may get wrong is the concept of unsubscribing and how best to do…

Once you get a hold of it, event-driven implementations using RxJS go a long way in making your Angular app dynamic. Even if you’re in your early days of using Angular, it’s highly recommended that you make use of Observables, Subjects, Subscriptions, etc. in your application to get the best out of the app. That said, if not done right, RxJS implementation can come back to you haunt you in the form of performance drops for your applications. And one of the top things even developers who have been into Angular for a while may get wrong is the concept of unsubscribing and how best to do it. That’s what we’ll be exploring in this post.

Table of Contents

Is unsubscribing necessary in the first place?

Absolutely. Granted, your observables and subscriptions will work just fine even if you don’t with the whole unsubscribing business. To the untrained eye, particularly in a small-ish application, everything may look good. Let me try and explain with a real-life analogy what unsubscribing can entail.

Consider that you’re at a restaurant. Before the meal comes in, you decide to wash your hands. You go to the washroom, turn the tap on, and press the soap dispenser. For some reason, the soap dispenser stays in the pressed down position and soap keeps dripping out. You wash your hands, and decide to leave the tap open — what’s the harm, someone will eventually come and close the tap and also fix the soap dispenser. Minutes later, when you’re enjoying your meal, or even an hour later when you’ve finished your filet mignon, left a handsome tip and stepped out, the tap in the washroom is still letting water out and so is the unchecked soap dispenser. See where I am going with this?

Although you never saw it, a lot of water was lost, and so was a lot of soap. There’s even a fair chance that the drain may get clogged up leading to a messy situation. This is pretty much what’d happen if you leave your observables open and unsubscribed after use. Unchecked memory leaks will happen during application runtime and may lead to issues although on the outside, at least for the initial part at least, you may be blissfully ignorant of the trouble to come.

To see this in action, subscribe to an observable in a component and keep logging the data from the observable. You’ll see that the logging will carry on.

angular subscription asynchronous

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Install Angular - Angular Environment Setup Process

Install Angular in easy step by step process. Firstly Install Node.js & npm, then Install Angular CLI, Create workspace and Deploy your App.

Preventing Angular Subscription Memory Leaks

Preventing Angular Subscription Memory Leaks. In my early Angular days, I would subscribe to observables without unsubscribing before the app was destroyed, possibly leading to a build up of memory leaks.

Basics of Angular: Part-1

What is Angular? What it does? How we implement it in a project? So, here are some basics of angular to let you learn more about angular. Angular is a Typesc

Preventing Angular Subscription Memory Leaks

In my early Angular days, I would subscribe to observables without unsubscribing before the app was destroyed, possibly leading to a build up of memory leaks.

Angular Sass: How To Use Sass In Angular 9 Tutorial

Sass in Angular is an extension of CSS that allows you to use things like variables, nested rules, inline imports. Angular supports Sass, CSS, and Less.