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