Getting Started with Firebase 9 #15 - Unsubscribing from Changes
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
GraphQL Tutorial #5 - Express App Setup
Net Ninja
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
GraphQL Tutorial #8 - Root Query
Net Ninja
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
GraphQL Tutorial #12 - Author Type
Net Ninja
GraphQL Tutorial #13 - Type Relations
Net Ninja
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
GraphQL Tutorial #18 - Mutations
Net Ninja
GraphQL Tutorial #19 - More on Mutations
Net Ninja
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
GraphQL Tutorial #23 - Create React App
Net Ninja
GraphQL Tutorial #24 - Book List Component
Net Ninja
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
GraphQL Tutorial #28 - Add Book Component
Net Ninja
GraphQL Tutorial #29 - External Query File
Net Ninja
GraphQL Tutorial #30 - Updating Component State
Net Ninja
GraphQL Tutorial #31 - Composing Queries
Net Ninja
GraphQL Tutorial #32 - query variables
Net Ninja
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
GraphQL Tutorial #34 - Book Details Component
Net Ninja
GraphQL Tutorial #36 - Styling the App
Net Ninja
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
Updated Vue & Firebase Course (Udemy)
Net Ninja
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja
More on: Database Integration
View skill →Related Reads
📰
📰
📰
📰
Memoization useMemo()Explained: Why React Doesn’t Need to Repeat the Same Work
Medium · JavaScript
Why Dark Mode Should Not Be a Second CSS File
Dev.to · Hasan Sarwer
Frontend-Only SaaS: The Rise of Static Utility Sites
Dev.to · yobox
Why I Built Yet Another JavaScript Date Picker
Dev.to · RollDate
🎓
Tutor Explanation
DeepCamp AI