Using Firebase with Node
Firebase-queue and worker
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
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.
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.
Install firebase-queue using npm in your nodejs app
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.
Update your Firebase Project Credentials and Firebase Database URL
or you can input firebase credentials directly as below
run server and worker seperately and test around with firebase queue
Hello World Firebase Realtime Database in Node
- First Go to Firebase Console and Create New Project.
- After Creating the project, in project click on settings icon besides project Name in left sidebar and select Permissions.
- On Permissions Page Click on Service accounts in left sidebar then click on Create Service Account
- In the popup window enter your service account name and choose Account Role and select Furnish a new private key and after that select JSON and click Create(Leave Enable Google App Domain-wide Delegation Unchecked).
- When you click create it will download a JSON file with your Account Credentials, just save the file Anywhere in your System.
- Next step is to Create a Database in your Firebase Console for which Go to Firebase Console and click on Database in left-sidebar. After that just create a new Database Object with Name user_data with some dummy value.
- Now your Firebase Database project is setup now simply copy following code in your project directory.
- Just change with the JSON Credentials file URL(For starters just copy the credentials file in Same folder and in index.js file just add the credentials File Name).
- Next step is to change the in index.js with actual Firebase Database URL, you will be able to find this URL in Firebase Console in Database Tab, The URL will be like https://.firebaseio.com/.
- The final step is to do
- After Executing above command NPM will install necessary packages required for Firebase. Finally to run and test project execute
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.