Getting Started with Firebase 9 #6 - Real Time Collection Data

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

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 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 set up a real-time subscription to a Firestore collection in Firebase 9, enabling real-time data updates in the console or a web application. The onSnapshot method is used to listen for changes to the collection and update the data accordingly.

Key Takeaways
  1. Import the necessary Firebase modules
  2. Set up a reference to the Firestore collection
  3. Use the onSnapshot method to listen for changes to the collection
  4. Handle the initial snapshot and subsequent updates
  5. Log the updated data to the console or render it in a web application
💡 The onSnapshot method allows for real-time data updates by listening for changes to the Firestore collection and updating the data accordingly.

Related Reads

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