Giter VIP home page Giter VIP logo

rxfire's Introduction

RxFire

Firebase and RxJS for all frameworks.

What is RxFire?

  • Observable creators - Observables bindings for most Firebase web libraries.
  • Portable - Use across any framework or no framework at all.
  • Tree shake-able - Import only what you need. Shake the rest out with your favorite module bundler like Webpack or Rollup.
  • Combine multiple data sources - Need to join two Firestore references? Want to combine an image from Cloud Storage with a Firestore document? Super easy with observables and operators.
  • Simplify code-splitting of Firebase - Using RxFire with Webpack makes it easy to load Firebase features on-demand.

Status: Beta

Install

# npm
npm i rxfire firebase rxjs --save
# yarn
yarn add rxfire firebase rxjs

Make sure to install Firebase and RxJS individually as they are peer dependencies of RxFire.

Example use:

import { initializeApp } from 'firebase/app';
import { getFirestore, collection, where, query } from 'firebase/firestore';
import { collectionData } from 'rxfire/firestore';
import { tap } from 'rxjs/operators';

const app = initializeApp({ /* config */ });
const firestore = getFirestore(app);
const citiesRef = query(
    collection(firestore, 'cities'),
    where('state', '==', 'CO')
);

collectionData(citiesRef, { idField: 'id' })
  .pipe(
    tap(cities => console.log('This is just an observable!'))
  )
  .subscribe(cities => { /* update UI */ })

Easily combine multiple Firebase data sources

RxJS provides multiple operators and creation methods for combining observable streams. This makes it easy to combine data from multiple Firebase resources. You can also handle simplify high asynchronous tasks like joins into a flat stream.

The example below streams a list of "cities" from Firestore and then retrieves their image from a Cloud Storage bucket. Both tasks are asynchronous but RxJS makes it easy to combine these tasks together.

import { initializeApp } from 'firebase/app';
import { getStorage, ref } from 'firebase/storage';
import { getFirestore, collection, where, query } from 'firebase/firestore';
import { collectionData } from 'rxfire/firestore';
import { getDownloadURL } from 'rxfire/storage';
import { combineLatest } from 'rxjs';
import { switchMap } from 'rxjs/operators';

const app = initializeApp({ /* config */ });
const firestore = getFirestore(app);
const storage = getStorage(app);
const citiesRef = query(
    collection(firestore, 'cities'),
    where('state', '==', 'CO')
);

collectionData(citiesRef, { idField: 'id' })
  .pipe(
    switchMap(cities => {
      return combineLatest(...cities.map(c => {
        const ref = ref(storage, `/cities/${c.id}.png`);
        return getDownloadURL(ref).pipe(map(imageURL => ({ imageURL, ...c })));
      }));
    })
  )
  .subscribe(cities => {
    cities.forEach(c => console.log(c.imageURL));
  });

Understanding RxFire imports

RxFire is a complementary library to Firebase. It is not meant to wrap the entire Firebase SDK. RxFire's purpose is to simplify async streams from Firebase. You need to import the Firebase SDK and initialize an app before using RxFire.

import { initializeApp } from 'firebase/app';
import { getStorage, ref } from 'firebase/storage';
import { getDownloadURL } from 'rxfire/storage';

const app = initializeApp({ /* config */ });
const storage = getStorage(app);
const ref = ref(storage, 'data.json');

// Now you can use RxFire!
const url$ = getDownloadURL(ref);

RxFire contains multiple entry points for module imports. Each Firebase library is an entry point.

import { } from 'rxfire/firestore';
import { } from 'rxfire/database';
import { } from 'rxfire/storage';
import { } from 'rxfire/auth';
import { } from 'rxfire/functions';
import { } from 'rxfire/performance';
import { } from 'rxfire/remote-config';

Simple functions

RxFire is a set of functions. Most functions create observables and from there you can use regular RxJS operators. Some functions are custom operators. But at the end of the day, it's all just functions. This is important for tree shaking. Any unused functions are stripped from your final build if you use a module bundler like Webpack or Rollup.

import { initializeApp } from 'firebase/app';
import { getStorage, ref } from 'firebase/storage';
import { getDownloadURL, put /* not used! */ } 'rxfire/storage';

const app = initializeApp({ /* config */ });
const storage = getStorage(app);
const ref = ref(storage, 'data.json');

const url$ = getDownloadURL(ref);

Documentation

Examples

Examples: See this example repository for a list of ways to configure and use RxFire.

rxfire's People

Contributors

feiyang1 avatar jamesdaniels avatar hsubox76 avatar davideast avatar renovate-bot avatar jhuleatt avatar google-oss-bot avatar renovate[bot] avatar jshcrowthe avatar dpebot avatar mikelehen avatar yuchenshi avatar hiranya911 avatar shelcindra avatar schmidt-sebastian avatar codediodeio avatar lucannez64 avatar ooookai avatar jmmendivil avatar yifanyang avatar swu0430 avatar samtstern avatar field123 avatar richieforeman avatar reimertz avatar jadengis avatar ifiokjr avatar angulartist avatar dungahk avatar ex37 avatar

Forkers

ironpark

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.