Getting Started with Firebase 9 #15 - Unsubscribing from Changes

Net Ninja · Beginner ·🌐 Frontend Engineering ·4y ago

Key Takeaways

The video demonstrates how to unsubscribe from Firebase 9 subscriptions, including real-time collection and document subscriptions, as well as authentication status changes, using unsubscribe functions returned by the onSnapshot method.

Full Transcript

all right my friend so there's one more thing i'd like to show you when it comes to firebase nine in this series and that is how to unsubscribe from subscriptions so currently we have three subscriptions set up we have one to the collection of data which sends us back a new snapshot every time there is a change in that collection one to a single document which sends us back a new document when there's a change to it and one to the authentication status which sends us back the user every time there's a status change so it's good practice to unsubscribe from any subscription when you no longer need it so i'm going to show you how we can do that for all three of these things right here so then to begin with i've already added on a new button at the bottom with the class of unsub and that says unsubscribe from db auth changes so we're going to unsubscribe from everything in one go when we click on this button right so over in the index.js i've also grabbed this unsub button using the query selector on this class and then i've added an event listener to it which is a click event so when we click on this we're going to fire some code all right so what we want to do in here is unsubscribe from all of the three things that were subscribed to so this one right here where we subscribe to all changes but also up here we have a real-time subscription to a single document if i can find it right at the top okay so this is the real-time data for a full collection i think the document is down here somewhere let me try and find it okay there it is so we have that subscription as well so we want to unsubscribe for all three of these so what we need to do is basically just capture the return of all of these subscriptions in some kind of variable because they all return a function and that function is an unsubscribe function so where we have the real time collection subscription i'm going to say const and then you can call this what you want it really doesn't matter i'm going to call it unsub call for collection and set it equal to this so this on snapshot returns to us an unsubscribe function and i'm storing that inside this constant i'm going to do the same thing for the document subscription down here so i'll say const unsub doc this time since we want to unsubscribe from a document and again this returns to us an unsubscribe function which is stored inside this constant and the same is true for the authentication status change one as well so this also returns a function which is an unsubscribe function so i can capture that in a constant as well and i'll say unsub auth for that one and set it equal to this so all we have to do is invoke all three of those functions inside here first of all i'll say console.log unsubscribing like so and then under that we'll do unsub call and invoke that function unsub doc and invoke that function and then finally on sub auth and invoke that function so now we're unsubscribing from all of those subscriptions all in one go when we click on that button so let me save this now and give it a whirl all right then so we can see this button in the browser now but i don't want to click it just yet because first of all i want to demonstrate that the subscriptions are still going on in the background so i'll add a new document first of all over here abc in def doesn't really matter and we can see that new snapshot that's a subscription let's try auth so i'm gonna try and log in as mario at netninja.dev and then test one two three four five and then log in and we can see down here the user so that subscription is still live and if we log out we can see it fires again but this time it's normal so these subscriptions are still going on in the background right now watch what happens if i click on this button then we can see unsubscribing right and now if i add in new documents then we shouldn't get a new snapshot because we've unsubscribed and we don't the same is true for this if i try to log in again then we no longer see that function fire and the user log to the console because we've unsubscribed from all state changes as well so that's how we unsubscribe from our subscriptions and remember it's good practice to do this whenever we don't need those subscriptions anymore all right then so that's pretty much it for this series of videos about firebase nine if you want to learn more definitely check out the docs because there are other features that you can use for example storage which is a way we can upload files to firebase hosting so we can host our websites using firebase and also cloud functions as well so we can run our own custom code on the backend so yeah if you want to learn more about firebase 9 definitely check out the docs so my friends i really really hope you enjoyed this series and you learned something along the way if you did please please please don't forget to share subscribe and like that really means a lot and if you want to access all of my youtube courses without adverts also get access to premium courses and early access courses as well you can do at netninja.dev you can sign up for net ninja pro which is just nine dollars a month and also half price for the first month with this promo code right here and for that like i said you get access to every course without adverts without youtube adverts you also get access to exclusive courses not found anywhere else you get access to my premium courses on udemy and also early access to all of my youtube courses as well so the link to this page to sign up is gonna be down below again i really hope you enjoyed this series and i'm gonna see you in the very next one [Music] you

Original Description

🐱‍👤 Course files: https://github.com/iamshaunjp/Getting-Started-with-Firebase-9 🐱‍👤 My Premium Udemy courses: https://thenetninja.co.uk/udemy/react-and-firebase https://thenetninja.co.uk/udemy/vue-and-firebase https://thenetninja.co.uk/udemy/modern-javascript https://thenetninja.co.uk/udemy/d3-and-firebase 🐱‍👤 Get access to all other premium courses on Net Ninja Pro: https://netninja.dev/ 🐱‍💻 My Social Links: Facebook - https://www.facebook.com/thenetninjauk Twitter - https://twitter.com/thenetninjauk Instagram - https://www.instagram.com/thenetninja/
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Net Ninja · Net Ninja · 0 of 60

← Previous Next →
1 Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
2 Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
3 Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
4 GraphQL Tutorial #1 - Introduction to GraphQL
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
5 GraphQL Tutorial #2 - A Birdseye View of GraphQL
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
6 GraphQL Tutorial #3 - Project (stack) Overview
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
7 GraphQL Tutorial #4 - Making Queries (front-end preview)
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
8 GraphQL Tutorial #5 - Express App Setup
GraphQL Tutorial #5 - Express App Setup
Net Ninja
9 GraphQL Tutorial #6 - Setting up GraphQL
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
10 GraphQL Tutorial #7 - GraphQL Schema
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
11 GraphQL Tutorial #8 - Root Query
GraphQL Tutorial #8 - Root Query
Net Ninja
12 GraphQL Tutorial #9 - The Resolve Function
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
13 GraphQL Tutorial #10 - Testing Queries in Graphiql
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
14 GraphQL Tutorial #11 - GraphQL ID Type
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
15 GraphQL Tutorial #12 - Author Type
GraphQL Tutorial #12 - Author Type
Net Ninja
16 GraphQL Tutorial #13 - Type Relations
GraphQL Tutorial #13 - Type Relations
Net Ninja
17 GraphQL Tutorial #14 - GraphQL Lists
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
18 GraphQL Tutorial #15 - More on Root Queries
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
19 GraphQL Tutorial #16 - Connecting to mLab
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
20 GraphQL Tutorial #17 - Mongoose Models
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
21 GraphQL Tutorial #18 - Mutations
GraphQL Tutorial #18 - Mutations
Net Ninja
22 GraphQL Tutorial #19 - More on Mutations
GraphQL Tutorial #19 - More on Mutations
Net Ninja
23 GraphQL Tutorial #20 - Updating the Resolve Functions
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
24 GraphQL Tutorial #21 - GraphQL NonNull
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
25 GraphQL Tutorial #22 - Adding a Front-end
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
26 GraphQL Tutorial #23 - Create React App
GraphQL Tutorial #23 - Create React App
Net Ninja
27 GraphQL Tutorial #24 - Book List Component
GraphQL Tutorial #24 - Book List Component
Net Ninja
28 GraphQL Tutorial #25 - Apollo Client Setup
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
29 GraphQL Tutorial #26 - Making Queries from React
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
30 GraphQL Tutorial #27 - Rendering Data in a Component
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
31 GraphQL Tutorial #28 - Add Book Component
GraphQL Tutorial #28 - Add Book Component
Net Ninja
32 GraphQL Tutorial #29 - External Query File
GraphQL Tutorial #29 - External Query File
Net Ninja
33 GraphQL Tutorial #30 - Updating Component State
GraphQL Tutorial #30 - Updating Component State
Net Ninja
34 GraphQL Tutorial #31 - Composing Queries
GraphQL Tutorial #31 - Composing Queries
Net Ninja
35 GraphQL Tutorial #32 - query variables
GraphQL Tutorial #32 - query variables
Net Ninja
36 GraphQL Tutorial #33 - Re-fetching Queries
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
37 GraphQL Tutorial #34 - Book Details Component
GraphQL Tutorial #34 - Book Details Component
Net Ninja
38 GraphQL Tutorial #36 - Styling the App
GraphQL Tutorial #36 - Styling the App
Net Ninja
39 GraphQL Tutorial #35 - Making a Single Query
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
40 Build Apps with Vue & Firebase - Udemy Course
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
41 Updated Vue & Firebase Course (Udemy)
Updated Vue & Firebase Course (Udemy)
Net Ninja
42 Vue & Firebase Real-time Chat (Preview) #1 - Intro
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
43 Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
44 Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
45 Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
46 Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
47 Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
48 Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
49 Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
50 Object Oriented JavaScript Tutorial #1 - Introduction
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
51 Object Oriented JavaScript Tutorial #2 - Object Literals
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
52 Object Oriented JavaScript Tutorial #3 - Updating Properties
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
53 Object Oriented JavaScript Tutorial #4 - Classes
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
54 Object Oriented JavaScript Tutorial #5  - Class Constructors
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
55 Object Oriented JavaScript Tutorial #6 - Class Methods
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
56 Object Oriented JavaScript Tutorial #7 - Method Chaining
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
57 Object Oriented JavaScript Tutorial #8 - Class Inheritance
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
58 Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
59 Object Oriented JavaScript Tutorial #10 - Prototype
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
60 Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja

This video teaches how to unsubscribe from Firebase 9 subscriptions, including real-time collection and document subscriptions, as well as authentication status changes, to optimize performance and prevent unnecessary data updates.

Key Takeaways
  1. Add an unsubscribe button to the UI
  2. Use the querySelector to grab the unsubscribe button
  3. Add an event listener to the unsubscribe button to trigger the unsubscribe function
  4. Capture the return of the onSnapshot method in a variable to store the unsubscribe function
  5. Invoke the unsubscribe function to unsubscribe from the subscription
💡 Unsubscribing from unnecessary subscriptions can help optimize Firebase Real-time Database performance and prevent unnecessary data updates.

Related Reads

Up next
How to Use Semrush Keyword Magic Tool with ChatGPT to Make Money
Grow with Will - SEO, Sales & Entrepreneurship
Watch →