firebase-nest
Utility to join multiple firebase paths and nested data into a single subscription.
Motivation
Apps often have the need to subscribe and unsubscribe from multiple firebase paths, as well as dynamically subscribe to additional paths as some data changes. For example, you might want to subscribe to several sources on user login (get user profile, get user friends, get user recent feed); and you might want to pull each friend's profile. Normally, you have to do a lot of dynamic subscription management, remembering to subscribe and unsubscribe from multiple firebase refs as your app state changes, and also as the master data (e.g. friend lists) changes.
Basic example
;;const fb = 'https://docs-examples.firebaseio.com';const subscribeSubsWithPromise subscribedRegistry = ;const unsubscribe promise = ;promise;
If we have 2 messages, one with uid=user1
, the other with uid=user2
, the parent messages subscription will subscribe to user1 and user2.
As data changes, childSubs are updated automatically.
Features
-
Declarative subscriptions
This lib allows an app to specify a logical data source as an array of declarative subscription specifications ("subs").
-
Promises
const {unsubscribe, promise} = subscribeSubsWithPromise(subs)
allows to know when initial data, including async child subscriptions, is loaded. -
Firebase permission-denied errors are handled via
onError
callbacks and promise rejections. -
Dynamic nested subscriptions
A sub corresponds to a firebase ref/query, and can have a
childSubs
that specifies how to subscribe to data for each child. -
Visualize subscription graphs
You can use
asNodesAndEdges(subscribedRegistry)
- produces{nodes, edges}
suitable for react-graph-vizOr
console.log(asDotGraph(subscribedRegistry))
- producesdigraph
dot output that can be saved to a file and fed to graphviz utils, likedot -Tpng yourFile.dot -o graph.png
-
RefCounted firebase refs
Support registering multiple subscriptions to the same source (identified by
subKey
). Underlying firebase on/off is only called once on the first subscribe/last unsubscribe. -
Composition
the subs can be easily composed and reused, as in the examples below.
-
Firebase query API support.
A sub is mapped to a Firebase ref/query (through
resolveFirebaseQuery
callback), soorderByChild
,startAt
,equalTo
etc. and all other firebase queries are supported. -
Value or List
onData
callbacks.
Subs with asValue
=true result in FB_VALUE callbacks:
Subs with asList
=true result in
then
//and FB_CHILD_REMOVED/CHANGED, as well as FB_CHILD_WILL_REMOVE/WILL_CHANGE
Usage
-
npm install firebase firebase-nest --save
-
Initialize the subscriber - generally should be a global/singleton
; const subscribeSubs = ;
- Create your subscription specifications, for example
const user1Subs = subKey: 'userDetail_user1' //can use any naming scheme you want to identify your logical sources asValue: true //or asList: true //optional: childSubs: ... to specify how to subscribe to data for each child //custom fields - can be anything you want, will be passed into onData & resolveFirebaseQuery callbacks path: 'https://your-firebase.com/users/user1' ;
Each sub needs to have a logical key ("subKey"), for example 'recent_feed_user1'. This is the key used for ref counting.
- Start listening to data
const unsub = ;
- Eventually unsub must be called to unsubscribe.
;
Mobx example
See https://github.com/nyura123/firebase-nest/blob/master/examples/MobxComponentExample.js for how to add dynamic firebase subscriptions and data to a React component.
Full Example
const nestedSubscriber = ;const Firebase = ; const subscribeSubs = ; { return subKey:"dinosaurScore_"+dinosaurKey path:"https://dinosaur-facts.firebaseio.com/scores/"+dinosaurKey asList:true //will work with asValue as well. asList generally has better performance for large datasets with small changes subKey:"dinosaurDetail_"+dinosaurKey path:"https://dinosaur-facts.firebaseio.com/dinosaurs/"+dinosaurKey asValue:true ;}; { return subKey: "allDinosaurs" path: "https://dinosaur-facts.firebaseio.com/dinosaurs" childSubs: dinosaurScoreAndDetailSubCreator //asValue will work as well. asList generally has better performance for large datasets with small changes asList: true ;} //A single subscription to subscribe to list of all dinosaurs, and detail/score for each oneconst unsub = ; //Eventually unsub() must be called
autoSubscriber can be used to automatically subscribe React components.
A component has to implement 2 methods: getSubs(props, state) that returns a sub or an array of subs subscribeSubs(subs, props, state) that actually performs the subscription - normally just calls nestedSubscriber's subscribeSubs
; ;; let dinosaurs;let reactiveComponent; const subscribeSubs subscribedRegistry = ; const globalSubscribeSubs = subscribeSubs; //Example usageconst fbRoot = "https://dinosaur-facts.firebaseio.com"; var DinosaurList = ;
asReduxMiddleware.js
; ; //example subscribe specs (subs) { return subKey: 'userDetail_' + userKey asValue: true params: name: 'users' key: userKey path: "https://my/path/to/users/"+userKey ;} { return subKey: 'friendListWithUserDetail_'+userKey asList: true childSubs: userDetailSubCreator params: name: 'friends' key: userKey path: "https://my/path/to/friends/"+userKey ;} { return subKey: 'feed'+userKey asList: true params: name: 'feed' key: userKey path: "https://my/path/to/feed/"+userKey ;} { return subKey: 'recent_likes_'+userKey asList: true params: name: 'likes' key: userKey orderByChild: "likedTs" startAtTs: now - 24*60*60*1000 path: "https://my/path/to/likes/"+userKey ;} { return recentLikesuserKey now;} //example actions - can easily compose subscriptions { return type: "FIREBASE_SUBSCRIBE" subs: ;} { return type: "FIREBASE_SUBSCRIBE" subs: ;} { return type: "FIREBASE_SUBSCRIBE" subs: ;} //Firebase data callbacks will be dispatched as FB_VALUE or FB_INIT_VAL,FB_CHILD_ADDED/REMOVED/CHANGED actions { return ;} { const subscribeSubs = ; return { return { }; }}