You have unlimited access as a PRO member
You are receiving a free preview of 3 lessons
Your free preview as expired - please upgrade to PRO
Contents
Recent Posts
- Object Oriented Programming With TypeScript
- Angular Elements Advanced Techniques
- TypeScript - the Basics
- The Real State of JavaScript 2018
- Cloud Scheduler for Firebase Functions
- Testing Firestore Security Rules With the Emulator
- How to Use Git and Github
- Infinite Virtual Scroll With the Angular CDK
- Build a Group Chat With Firestore
- Async Await Pro Tips
Introduction to RxFire - RxJS for Firebase
Episode 131 written by Jeff DelaneySource code for Introduction to RxFire - RxJS for Firebase on Github
Maintaining async callback-based code is one of the most challenging tasks a developer will face. Over the past few years, RxJS has become the leading tool for reactive programming in JavaScript, so it only makes sense that Firebase would leverage it to make realtime streams more developer-friendly. In the following lesson, you will take an early look at a new officially-supported library called RxFire.
Principles
- Not a replacement for AngularFire2 in Angular projects.
- Ideal for projects using Firebase and RxJS.
- Uses pure functions that return Observables.
- Makes Lazy Initialization possible.
The first requirement Firebase and RxJS as peer dependencies.
npm i rxfire firebase rxjs |
Common Techniques
There is a repo dedicated to RxFire examples that you should clone to see it in action in a variety of settings.
Let’s start by looking at some isolated examples of the most common tasks you might perform with RxFire.
Observe the auth state of the current user
import { authState } from 'rxfire/auth'; |
Query a collection from Firestore and get an array of data & IDs
The collectionData
function takes a query, then returns the snapshot mapped to a plain object. You can also get the documument IDs by simply passing a string as the second arg.
import { collectionData } from 'rxfire/firestore'; |
Upload a File to Firebase Storage
import { put, getDownloadURL } from 'rxfire/storage'; |
Switch to a Different Observable Stream for Relational Data
Let’s imagine we have one document for an Animal and another for its feeding schedule. We need to read that animal’s ID before retriving the food document.
import { docData } from 'rxfire/firestore'; |
We only scratched the surface with these examples, but that should give you an idea about how RxFire works.
Build Reactive Firebase Web Components with Stencil
Stencil is an awesome tool from the Ionic team used for composing web components. In fact, Ionic v4 is completely built by Stencil. If you’re not familiar with it, don’t feel too intimidated - it’s the most approachable way to build web components in my opinion.
Our component is a very basic todo list that requires Google authentication. And because it does not take Firebase as a hard dependency, it can work with any app that uses Firebase by picking up an existing config.
Initial Setup
npm init stencil --run |
Our component will expect a Firebase app to be initialized, so let’s take care of that in the index.html
<head> |
Google OAuth with RxFire
The @State
decorator in stencil will re-render the component whenever its value changes. This works really well with RxFire because we can have our Observables tied to the component state, ensuring the latest data is always presented in the UI.
/// <reference types="firebase" /> |
Adding a ToDo List for the Current User
Let’s expand the component by building a todo list for the user. Notice how we use switchMap
to grab the current users uid, then use it to query Firestore for their associated todo documents.
// ... |
The End
You just built a reactive fullstack web component that can integrate with any JS framework or Firebase project. Web components and RxJS will play a huge role in the future of the web, and RxFire is a powerful tool for developers building realtime apps.