Getting Started with Firebase 9 #6 - Real Time Collection Data
Key Takeaways
The video demonstrates how to set up a real-time subscription to a Firestore collection using the onSnapshot method in Firebase 9, allowing for real-time data updates in the console or a web application.
Full Transcript
all right then gang so we've seen now how we can get data and we're logging that data to the console and also add new data and delete data as well from a collection but at the minute if we were to add new data or delete data then in order to see the updated collection data in the console we'd have to refresh the page right let me demo that if i add in a new book so the way of kings again and then brandon sanderson like so hit enter it adds the bulk but we don't see that new data over here right away we have to refresh the page in order to see that data now if we were rendering this data to the browser so that it output on the screen in some kind of template then the same would be true we'd have to refresh the page after we added a new book in order to see that in the browser right so instead what we can do is we can set up what's known as a real-time listener or a subscription to the firestore collection which actively listens for changes to that collection so that if i was to add a new book or change book or delete a book from that collection then it would send back the updated snapshot or the updated data inside that collection right away once that happens and then in our case it would just be reloading it to the console but in your case if you're making a website with firebase then it might re-render the template to the browser okay so i'm going to show you how we can set up this real-time listener or subscription to a firestore collection in this lesson okay then so currently we are getting the documents right here using this get docs function and like we just explored this get docs function just runs once to get the documents when we run the code just once but if there's a change later on in the data it doesn't automatically run again to go out and fetch the new data it just runs once and instead we want to set up a subscription to the collection so that every time there is a change in the data in that collection we get that data back right away so it sends us back a new snapshot every time there's a change right so what i'm going to do is change this to a real time collection data because that's what we're going to set up now and then up here i'm going to replace get docs with a different function called on snapshot which is how we set up a real-time listener using this on snapshot function so then what i'm going to do is basically rewrite this code right here but using that new function so then let's do that down here i'm going to say on snapshot which is that function we just imported and then inside here we need to pass in two arguments we say call ref just like we did up here because we need to pass in a reference to a collection that we want to listen to or set up a subscription to and then as a second argument we pass in a function and this function is gonna fire every time there is a change in the collection and it sends us back a new snapshot and also wants initially as well so like this runs once initially this is going to run once initially as well to get the data to begin with but then it's also going to run thereafter this function every time there is a change in the collection that we're listening to and it's going to send us back a new snapshot of that collection after the change occurs right so unlike this one when we have a then method we don't have a then method on here we'll just fire this function whenever there's a change so inside here we take in the snapshot just like we do up here and then that snapshot is going to be pretty much the same as this kind of snapshot so inside this function the logic all this stuff is going to be the same we can set up a new array called books we can cycle through the documents on the snapshot and for each document we add a new object to the books array where we grab the data of the document and also the id of the document and then finally we log those to the console so what i'm going to do is actually just cut all of this and i'm going to paste it in right here instead and let me just scoot this back all right and that's all there is to it my friends so now i can delete all this where we just get the data once and instead we're using this on snapshot function to listen to the data so this function is going to go out and grab the data once to begin with and run this function when it gets it so we log out the books to the console when we first load the page but thereafter if i was to add a new book or delete a book that would trigger firestore to send us back a new snapshot and this function would run again so that we can output the books to the console again the new updated data all right so i hope that all makes sense so let me save this now and we'll give it a whirl in the browser all right then so when we first load the page if i refresh you can see it's performing that initial fetch to get the first initial snapshot of the collection and we get these four documents right here now watch what happens if i try to delete one so this the wise man's favor for example i'm just going to grab this id and then i'm going to paste it in here to delete the document so as soon as i click on this that should send out a request to delete the document from the collection in firestore it will delete it and then that will trigger firestore to send us back a new snapshot the function is going to re-run in our code with that snapshot and it's going to log out the new data to the console so we should see three books this time as soon as i delete the book and that's finished so let me click this and now we can see three books in this array and if we open this up we can see the wise band fare is no longer there awesome now the same is true for if i add any books so i can say the wise man's fit up here and then it's by patrick rothfuss press enter to add a new book we should get a new snapshot back and we do and we can see that right here four elements inside it including the wise man's fear awesome so that my friends is how we set up a real-time listener or a subscription to a firestore collection to get real-time data back as events occur in the collection
Original Description
Hey gang, in this firebase 9 tutorial I'll show you how to setup a real-time subscription to a collection get real-time data, using the onSnapshot method.
🐱👤 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
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