System Requirements:
Getting Started
//Loading Firebase Package
var firebase = require("firebase");
/**
* Update your Firebase Project
* Credentials and Firebase Database
* URL
*/
firebase.initializeApp({
serviceAccount: "<path to Firebase Credentials Json File>",
databaseURL: "<Firebase Database URL>"
}); //by adding your credentials, you get authorized to read and write from the database
/**
* Loading Firebase Database and refering
* to user_data Object from the Database
*/
var db = firebase.database();
var ref = db.ref("/user_data"); //Set the current directory you are working in
/**
* Setting Data Object Value
*/
ref.set([
{
id:20,
name:"Jane Doe",
email:"[email protected]",
website:"https://jane.foo.bar"
},
{
id:21,
name:"John doe",
email:"[email protected]",
website:"https://foo.bar"
}
]);
/**
* Pushing New Value
* in the Database Object
*/
ref.push({
id:22,
name:"Jane Doe",
email:"[email protected]",
website:"https://jane.foo.bar"
});
/**
* Reading Value from
* Firebase Data Object
*/
ref.once("value", function(snapshot) {
var data = snapshot.val(); //Data is in JSON format.
console.log(data);
});
npm install firebase
node index.js
What does the project actually do?
The project loads the Data from cloud based Firebase Database. The project also demonstrates how to Write and Read data from a Firebase Data Object.
In order to view your data get updated in realtime, go to your console click on the project you made, and on the left, hit Database. There, you can see your data get updated in real-time, along with their values.
You can push tasks or data to the firebase realtime database and run a worker which listens to the firebase queue to run some background processess
Setup firebase
Create a Firebase project in the Firebase console, if you don't already have one. If you already have an existing Google project associated with your app, click Import Google Project. Otherwise, click Create New Project..
Click settings icon and select Permissions.
Select Service accounts from the menu on the left.
Click Create service account.
Enter a name for your service account.
You can optionally customize the ID from the one automatically generated from the name.
Choose Project > Editor from the Role dropdown.
Select Furnish a new private key and leave the Key type as JSON.
Leave Enable Google Apps Domain-wide Delegation unselected.
Click Create
When you create the service account, a JSON file containing your service account's credentials is downloaded for you. You'll need this to initialize the SDK in the server.
Setup server
Install firebase-queue using npm in your nodejs app
npm install firebase firebase-queue --save
Once you've installed firebase and firebase-queue, you can get started by creating a new Queue and passing it your Firebase reference and a processing function.
Now lets create a firebase queue task from the app when a new user is created and set worker to listen for firebase-queue task and send an email to the created users mail.
*server.js
var app=express();
var Queue = require('firebase-queue'),
Firebase = require('firebase');
Update your Firebase Project Credentials and Firebase Database URL
var firebase = Firebase.initializeApp({
serviceAccount: "path/to/serviceAccountCredentials.json",
databaseURL: "https://databaseName.firebaseio.com"
});
or you can input firebase credentials directly as below
var firebase = Firebase.initializeApp({
serviceAccount: {
projectId: "projectId",
clientEmail: "[email protected]",
privateKey: "-----BEGIN PRIVATE KEY-----\nkey\n-----END PRIVATE KEY-----\n"
},
databaseURL: "https://databaseName.firebaseio.com"
});
var refQueue = firebase.database().ref("queue/tasks");
createUser = funtion(email, password){
var user = {
username: email,
password: password
};
user = new db.users(user);
user.save(function(err, user){
if(!err){
refQueue.push({case: "NEW_USER", data: user});
}
})
}
createUser("[email protected]", "password");
*worker.js
var Queue = require('firebase-queue'),
Firebase = require('firebase');
//Update your Firebase Project Credentials and Firebase Database URL by one of the way specified in server.js
var firebase = Firebase.initializeApp({
serviceAccount: "path/to/serviceAccountCredentials.json",
databaseURL: "https://databaseName.firebaseio.com"
});
var refQueue = firebase.database().ref("queue");
var queue = new Queue(refQueue, function(data, progress, resolve, reject) {
switch(data.case){
case "NEW_USER":
sendMail(data.data.email);
console.log("user created");
//sendMail function is not an inbuilt function and will not work unless you define and implement the function
break;
// Finish the task asynchronously
setTimeout(function() {
resolve();
}, 1000);
});
run server and worker seperately and test around with firebase queue
node server.js
node worker.js