Skip to content

Instantly share code, notes, and snippets.

@asciimike
Created December 3, 2014 22:20
Show Gist options
  • Save asciimike/6edf257062f433f89976 to your computer and use it in GitHub Desktop.
Save asciimike/6edf257062f433f89976 to your computer and use it in GitHub Desktop.
Mirror a Firebase to a second Firebase
var Firebase = require('firebase');
var fromRef = new Firebase('https://<FROM-FIREBASE>.firebaseio.com');
var toRef = new Firebase('https://<TO-FIREBASE>.firebaseio.com');
function main(){
mirrorRef(fromRef,toRef);
}
function mirrorRef(fromReference, toReference){
fromReference.on('child_added', function(snapshot){
if (snapshot.hasChildren()){
mirrorRef(fromReference.child(snapshot.key()),toReference.child(snapshot.key()));
} else {
snapshot.ref().on('value', function(child_snap){
toReference.child(child_snap.key()).set(child_snap.val());
});
}
});
fromReference.on('child_removed', function(snapshot){
if (snapshot.hasChildren()){
mirrorRef(fromReference.child(snapshot.key()),toReference.child(snapshot.key()));
} else {
toReference.child(snapshot.key()).remove();
snapshot.ref().off();
}
});
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment