Last active
October 18, 2016 03:00
-
-
Save kkm/0eee91c6b63ddfe94a45fe955c0b81d8 to your computer and use it in GitHub Desktop.
firebase in realtime
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var firebase = Meteor.npmRequire("firebase"); | |
| var values = new Mongo.Collection("values"); | |
| var databaseName = 'values/'; | |
| firebase.initializeApp({ | |
| databaseURL : "https://xxxx.firebaseio.com/", | |
| serviceAccount : "/home/kkm/firebase/server/serviceAccountCredentials.json" | |
| }); | |
| //Mongo RESET | |
| values.remove({}); | |
| //DB Init | |
| var ref = firebase.database().ref(databaseName); | |
| //Firebase Incoming Events | |
| //FIREBASE ADDED | |
| ref.orderByChild("ts").on("child_added", function(snapshot) { | |
| console.log("firebase - added:", snapshot.val()) | |
| Fiber(function() { | |
| try { | |
| values.insert(snapshot.val()); | |
| } catch(err) { | |
| console.log("Already added:", snapshot.val()._id); | |
| } | |
| }).run(); | |
| }, function(errorObject) { | |
| console.log("The read failed: " + errorObject.code); | |
| }); | |
| //FIREBASE REMOVE | |
| ref.on("child_removed", function(snapshot) { | |
| console.log("firebase - removed:", snapshot.val()) | |
| Fiber(function() { | |
| values.remove(snapshot.val()); | |
| }).run(); | |
| }); | |
| //FIREBASE changed | |
| ref.on("child_changed", function(snapshot) { | |
| console.log("firebase - changed:", snapshot.val()) | |
| Fiber(function() { | |
| values.update(snapshot.val()._id, {$set : snapshot.val()}); | |
| } | |
| }).run(); | |
| }); | |
| //Meteor Method with firebase and Future | |
| var Future = Npm.require('fibers/future'); | |
| Meteor.methods({ | |
| "getDataFirebase" : function(id) { | |
| var future = new Future(); | |
| var ref = firebase.database().ref(databaseName + id); | |
| ref.once("value", function(snapshot) { | |
| return future.return(snapshot.val()); //Wartet und endlich Wert zurück. | |
| }, function(errorObject) { | |
| console.log("The read failed: " + errorObject.code); | |
| }); | |
| return future.wait(); | |
| } | |
| }); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
INSERT:
var rnd = Random.id();
var ref = firebase.database().ref(databaseName + rnd);
ref.set({ts : +(new Date), data : value, _id : rnd});