Top 7 RxJS Concepts for Angular Developers

Fireship · Beginner ·🌐 Frontend Engineering ·8y ago

Key Takeaways

The video covers 7 essential RxJS concepts for Angular developers, including instance and static methods, subscriptions, mapping observables, switchMap operator, combining observables, and Behavior Subjects.

Full Transcript

rxjs is a huge library with a very steep learning curve in this episode I'm going to talk about a few key Concepts that are specific to angular Firebase developers I believe it's best to focus on what you actually need to learn rather than try to master the entire rxjs library to do this I've created a simple component that uses the angular Fire 2 package to retrieve cats and dogs from Firebase cats and dogs each have their own collection and there's another collection for humans which represent the owner of a given pet your component should look something like this we import the angular fire database and then inject it in the Constructor when using rxjs and angular you only import the methods and classes that you need in rxjs there are two types of methods static and instance in this first example we use an instance method combine latest so we create two instances of an observable then we can use that operator on the observable instance itself notice how the method was imported from ad SL operator the operator directory contains all of the instance methods it's not important to know at this point but using combined latest will result in an observable array of two strings now let's accomplish the same thing using a static method to obtain the static method we import from the add/ observable directory instead of calling the method on an instance we call it on the observable class itself and then we pass the two observables to it as arguments in rxjs some methods are only instance others are only static and some overlap for both now let's talk about subscriptions that's how you actually extract data from observables there are two main ways to do this in angular you can either subscribe in the component typescript or in the component HTML in this example we will subscribe to cats in the HTML and dogs in the component typescript notice how cats are a Firebase list observable and dogs are just a regular array calling DB list will return in observable from the Firebase database for cats we just keep it as is but for dogs we call subscribe which will cause the observable to emit an array of dogs that we can set as the variable on this component to subscribe to cats we do this in the template Itself by using the async pipe provided by angular under the hood this is subscribing to the observable just like we did in the typescript for dogs it's generally preferred to use the async pipe when possible because it will also unsubscribe to the observable to prevent memory leaks and it just results in Plus Code when we iterate over dogs notice we're not using the async pipe since that was already unwrapped in the component if we go into the app we can see the results are identical for both collections when you create subscriptions it's a good practice to unsubscribe when that data is no longer needed if you use the async pipe like we did with cats this will happen automatically but with dogs we built the subscription manually so we want to end that subscription when the component's no longer in use we can do this by setting the subscription to its own variable then listen for the on destroy life cycle hook and call unsubscribe the next concept I want to talk about is mapping observables this allows you to transform Data before you subscribe to it the first thing we'll do is transform a list observable of cats into a number based on the length of that collection so first we obtain the list observable from Firebase then we call map and that will emit an array which we can just call length on and that'll return a number representing the length of that collection in the next example we take a dog as an object observable then map its name just to a single string as you can see the map operator is very powerful you can use it to transform any type of asynchronous data into the format that you want going back to the HTML we can just call these observables with the async pipe and get their mapped values back going back into the app we can see the observable is transformed into a string and a number as expected now let's talk about the switch map operator this allows us to handle data relationships that exist between two independent streams a common example in Firebase is when you need the oth observable to emit the current user before you can query some other data we're going to simulate that situation here by taking an object observable of a human then use the name from that observable to query the pets owned by that human so switch map is kind of like subscribing to the first observable and then using that data to return a second observable so we set this whole thing equal to the dogs variable then iterate over it in the template like normal with the async pipe back in the app we can see the dogs observable only emits the two dogs that match this human owner name in some cases you might find yourself needing to combine two observables into one rxjs has several different combination operators but we'll just be focusing on combine latest for this example we will pull two different object observables from Firebase and then combine them into a new observable called Animals in this case I used the static method from the observable class to pass both of these object observables as arguments this results in a observable with an array that we can iterate over in the template if we go back to the app we can see we have one cat and one dog in an array even though they come from completely different collections in Firebase the final topic I want to cover is about Behavior subjects which allow us to share current data throughout an angular app I'll be going over this very quickly so I also recommend checking out the JavaScript evangelist and his video on Behavior subjects check out the link in the description a behavior subject is just an observable that we can push data to it also has a concept of a current value which is very useful for real-time app development because you generally want the most up-to-date information displayed in the app in this example we're going to create a new variable called current dog which is a behavior subject and it gets updated whenever you click on the dog's image this may seem trivial in this example but when you use a behavior subject in an angular service you can then share current data throughout the application we push new data to the behavior subject by calling next and that will update any observers throughout the app in the template we allow the user to change the current dog or the value of the behavior subject by clicking on a dog's image then we unwrap the behavior subject with the async pipe just like we with any other observable but also notice how we use the question mark before calling any attributes cuz the default value is null so we don't want to call an attribute that doesn't exist now if we go back into the app we can see when we click on a dog's image it updates the current dog with that [Music] data that's it for my top seven rxjs patterns for angular Firebase developers if this video helped you please like And subscribe and if you want to support the channel consider becoming a pro member at angular firebase.com thanks for watching and I'll see you next [Music] time

Original Description

Learn 7 essential tips when using RxJS with Angular and Firebase, each with its own example. Full lesson: https://angularfirebase.com/lessons/top-7-rxjs-patterns-for-angular-development/ JavaScript Evangelist BehaviorSubject: https://youtu.be/IpM3oUPuGME Learn about RxJS at https://learnrxjs.io
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Fireship · Fireship · 30 of 60

1 Angular 4 Development and Production Environments with Firebase
Angular 4 Development and Production Environments with Firebase
Fireship
2 OAuth with Angular and Firebase Tutorial
OAuth with Angular and Firebase Tutorial
Fireship
3 Anonymous Authentication with Angular and Firebase - Lazy Registration
Anonymous Authentication with Angular and Firebase - Lazy Registration
Fireship
4 Angular Router Guards for Firebase Users
Angular Router Guards for Firebase Users
Fireship
5 Angular Firebase CRUD App with NoSQL Database Tutorial
Angular Firebase CRUD App with NoSQL Database Tutorial
Fireship
6 Upload Files from Angular to Firebase Storage
Upload Files from Angular to Firebase Storage
Fireship
7 How to Deploy an Angular App to Firebase Hosting
How to Deploy an Angular App to Firebase Hosting
Fireship
8 Sharing Data between Components in Angular
Sharing Data between Components in Angular
Fireship
9 Loading Spinners for Asynchronous Firebase Data
Loading Spinners for Asynchronous Firebase Data
Fireship
10 Angular 4 Transactional Email with Google Firebase Cloud Functions
Angular 4 Transactional Email with Google Firebase Cloud Functions
Fireship
11 Firebase Database Rules Tutorial
Firebase Database Rules Tutorial
Fireship
12 Autocomplete Search with Angular4 and Firebase
Autocomplete Search with Angular4 and Firebase
Fireship
13 Reddit Inspired Upvoting System with Angular and Firebase NoSQL
Reddit Inspired Upvoting System with Angular and Firebase NoSQL
Fireship
14 Angular Drag-and-Drop File Uploads to Firebase Storage
Angular Drag-and-Drop File Uploads to Firebase Storage
Fireship
15 Text Translation with Firebase Cloud Functions onWrite and Angular 4
Text Translation with Firebase Cloud Functions onWrite and Angular 4
Fireship
16 Custom Usernames with Firebase Authentication
Custom Usernames with Firebase Authentication
Fireship
17 Twitter-Inspired Follow Unfollow Feature with Firebase and Angular 4
Twitter-Inspired Follow Unfollow Feature with Firebase and Angular 4
Fireship
18 Simple Pagination with Firebase and Angular 4
Simple Pagination with Firebase and Angular 4
Fireship
19 How to Connect Firebase Users to their Data - 3 Methods
How to Connect Firebase Users to their Data - 3 Methods
Fireship
20 Add Toast Message Notifications to your Angular App
Add Toast Message Notifications to your Angular App
Fireship
21 Facebook-Inspired Reactions System with Angular and Firebase
Facebook-Inspired Reactions System with Angular and Firebase
Fireship
22 Learn NgModule in Angular with Examples
Learn NgModule in Angular with Examples
Fireship
23 Lazy Loading Components in Angular 4
Lazy Loading Components in Angular 4
Fireship
24 Stripe Checkout Payments with Angular and Firebase - Part 1
Stripe Checkout Payments with Angular and Firebase - Part 1
Fireship
25 Process Stripe Payments with Firebase Cloud Functions - Part 2
Process Stripe Payments with Firebase Cloud Functions - Part 2
Fireship
26 Selling Digital Content in Angular with Stripe Payments - Part 3
Selling Digital Content in Angular with Stripe Payments - Part 3
Fireship
27 Angular 4 Full Text Search with Algolia - Part 1
Angular 4 Full Text Search with Algolia - Part 1
Fireship
28 Algolia with Firebase Cloud Functions - Part 2
Algolia with Firebase Cloud Functions - Part 2
Fireship
29 Firebase Phone Authentication in Angular 4
Firebase Phone Authentication in Angular 4
Fireship
Top 7 RxJS Concepts for Angular Developers
Top 7 RxJS Concepts for Angular Developers
Fireship
31 Learn Angular Animations with 5 Examples
Learn Angular Animations with 5 Examples
Fireship
32 Advanced Firebase Data Filtering (Multi-Property)
Advanced Firebase Data Filtering (Multi-Property)
Fireship
33 Realtime Maps with Mapbox + Firebase + Angular
Realtime Maps with Mapbox + Firebase + Angular
Fireship
34 Angular Reactive Forms with Firebase Database Backend
Angular Reactive Forms with Firebase Database Backend
Fireship
35 Send Push Notifications in Angular with Firebase Cloud Messaging
Send Push Notifications in Angular with Firebase Cloud Messaging
Fireship
36 Top 7 Ways to Debug Angular 4 Apps
Top 7 Ways to Debug Angular 4 Apps
Fireship
37 Infinite Scroll with Angular and Firebase
Infinite Scroll with Angular and Firebase
Fireship
38 Use TypeScript with Firebase Cloud Functions
Use TypeScript with Firebase Cloud Functions
Fireship
39 Realtime Graphs and Charts with Plotly and Firebase
Realtime Graphs and Charts with Plotly and Firebase
Fireship
40 Role-Based User Permissions in Firebase
Role-Based User Permissions in Firebase
Fireship
41 User Presence System in Realtime - Online, Offline, Away
User Presence System in Realtime - Online, Offline, Away
Fireship
42 Location-based Queries with GeoFire and Angular Google Maps
Location-based Queries with GeoFire and Angular Google Maps
Fireship
43 Angular ngrx Redux Quick Start Tutorial
Angular ngrx Redux Quick Start Tutorial
Fireship
44 Angular Ngrx Effects with Firebase Database
Angular Ngrx Effects with Firebase Database
Fireship
45 Progressive Web Apps with Angular
Progressive Web Apps with Angular
Fireship
46 Angular Ngrx with Firebase Google OAuth User Authentication
Angular Ngrx with Firebase Google OAuth User Authentication
Fireship
47 RxJS Quick Start with Practical Examples
RxJS Quick Start with Practical Examples
Fireship
48 Send SMS Text Messages with Twilio and Firebase
Send SMS Text Messages with Twilio and Firebase
Fireship
49 Firebase Database Performance Profiling
Firebase Database Performance Profiling
Fireship
50 Native Desktop Apps with Angular and Electron
Native Desktop Apps with Angular and Electron
Fireship
51 Subscription Payments with Stripe, Angular, and Firebase
Subscription Payments with Stripe, Angular, and Firebase
Fireship
52 Firestore with AngularFire5 Quick Start Tutorial
Firestore with AngularFire5 Quick Start Tutorial
Fireship
53 Angular HTTP Client Quick Start Tutorial
Angular HTTP Client Quick Start Tutorial
Fireship
54 Google Sign-In with Firestore Custom User Data
Google Sign-In with Firestore Custom User Data
Fireship
55 Star Review System from Scratch with Firestore + Angular
Star Review System from Scratch with Firestore + Angular
Fireship
56 Angular Chatbot with Dialogflow (API.ai)
Angular Chatbot with Dialogflow (API.ai)
Fireship
57 Learn @ngrx/entity and Feature Modules
Learn @ngrx/entity and Feature Modules
Fireship
58 Infinite Scroll Pagination with Firestore
Infinite Scroll Pagination with Firestore
Fireship
59 Faster Firestore via Data Aggregation
Faster Firestore via Data Aggregation
Fireship
60 Contentful - CMS for Angular Progressive Web Apps
Contentful - CMS for Angular Progressive Web Apps
Fireship

Learn the top 7 RxJS concepts for Angular developers, including instance and static methods, subscriptions, mapping observables, and Behavior Subjects. Mastering these concepts will improve your skills in using RxJS with Angular and Firebase.

Key Takeaways
  1. Import the necessary methods and classes from RxJS
  2. Use instance methods like combineLatest
  3. Use static methods like combineLatest
  4. Create subscriptions to observables
  5. Map observables to transform data
  6. Use the switchMap operator to handle data relationships
  7. Combine observables into one
  8. Use Behavior Subjects to share current data throughout an Angular app
💡 Using the async pipe to subscribe to observables is generally preferred because it will also unsubscribe to prevent memory leaks.

Related AI Lessons

Up next
The masks we wear | Zora Krstić | TEDxLuxembourgCity
TEDx Talks
Watch →