I had the same issue in the production environment. Sometimes the node-js server(desktop application) does not connect to firebase's real-time database from starting(installing) then after a few days I try again and it starts working. I don't know what can cause this? I tried disabling the firewall, I have allowed apps through the firewall.
when these issues occur in the production environment I switch my database to MongoDB database it always works really well.
This happed first time on my own laptop today
I was starting a new project so I have written simple code as below but was not able to connect to a real-time database for some reason but no error!
var admin = require("firebase-admin");
var serviceAccount = require("./gerneral/service.json");
admin.initializeApp({
serviceAccountId: admin.credential.cert(serviceAccount),
databaseURL: "databseurl"
});
const db = admin.database();
var ref = db.ref("data");
const connectedRef = admin.database().ref('.info/connected');
connectedRef.on('value', (snap) => {
if (snap.val() === true) {
console.log('connected');
} else {
console.error('not connected');
}
});
ref.set("it is not")
it output only ==> not connected
& not adding any value to firebase real-time database
& no error
I have used mobile JIO internet (good).
I have disabled the firewall
I have double-checked the service account(JSON file) and database URL and rules of the database.
I have a different project(related to the firebase Realtime database) on my PC I run that project and it running perfectly fine.
what can cause this?
how can I solve this issue?
Additional Information:
I try Using this project's database in other firebase-related projects (which is working fine). It shows this project's database connecting and getting data.
so this issue is most probably related to firewall or security but I double-check after disabling the firewall so why not connect to firebase's DB?
Please anyone how to find the cause of the issue?
Thank you in advance.
Related
Operating System: Ubuntu 16.04
Firebase SDK version: 4.8.0
Library version 5.5.1
Node version: 7.8.0
Firebase Product: database
Hey! Yesterday my server with Admin SDK stopped working. After a little investigation it turns out that events don't fire anymore. For example, the following code neither fails or succeedes (the process is running forever). It is run on a local dev server:
const admin = require('firebase-admin')
const config = require('./config')
const signature = require('./signature.json')
admin.initializeApp({
credential: admin.credential.cert(signature),
databaseURL: config.databaseURL
})
admin.database()
admin.database().ref('/config').once('value')
.then(snapshot => console.log(snapshot.val()))
.catch(error => console.log(error))
module.exports = {}
Database events are working when I subscribe to them as a client. Also database is accesible from web console. I've tried generating a new private key (credentials), but it doesn't help. What am I missing? Is there a way to take a closer look at what's going on? Maybe enabling debug mode somehow?
After enabling loggin I was able to figure out what's wrong.
// Enable logging
admin.database.enableLogging(true)
The problem was that the time on my machine was out of sync. After synchronising it the database is working again.
Using the Node.js admin SDK with Firebase Functions I get a timeout whenever I try to access the Realtime Database. This occurs only when testing a function locally (firebase serve --only functions,hosting) and when the default app is initialized using the functions.config().firebase.
This is a new behavior that started just a couple a days ago. However, if I try to initialize the default app with the serviceAccount.json file everything works as expected.
I'm using firebase-admin version 4.2.1 and firebase-functions version 0.5.9.
I wrote a straight forward http triggered function that fails due to timeout:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
const db = admin.database();
exports.testDbConnection = functions.https.onRequest((req, res) => {
return admin.database().ref().once('value')
.then(function(snapshot) {
res.json(snapshot);
}).catch(function(error) {
res.json(error);
});
});
from the documentation
Always end an HTTP function with send(), redirect(), or end(). Otherwise, your function might to continue to run and be forcibly terminated by the system
see https://firebase.google.com/docs/functions/http-events#terminate_http_functions
This might depend on the firebase-tools version that you are using, but looks familiar to this Github issue
The solution for it is to either upgrade to the latest version of the CLI or use the workaround solution:
Go to https://cloud.google.com/console/iam-admin/serviceaccounts
Click “Create service account”, give it a name (e.g. emulator), give it the Project>Owner role.Check “Furnish a new private key”, pick “JSON”.
Save the file somewhere on your computer
Run export GOOGLE_APPLICATION_CREDENTIALS="absolute/path/to/file.json"
Run firebase serve --only functions
I have installed and run firebase admin on my node server, it has been running well until today, there is no given error to tell what happened, it simply stops working.
var admin = require("firebase-admin");
var serviceAccount = require("mycom-firebase-adminsdk-d1ebt123456.json");
var app = FireBaseAdapter.admin.initializeApp({
credential: FireBaseAdapter.admin.credential.cert(serviceAccount),
databaseURL: "https://xxxx.firebaseio.com"
});
var db = app.database();
var ref = db.ref("messages"); // this node actually exists in my db.
ref.on("value", function(snapshot) {
// this will never be called - which has been working before.
console.log(snapshot.val());
}, function (errorObject) {
console.log("The read failed: " + errorObject.code);
});
I even wrap it in the try catch to print out the error, but there isn't any. I can log into the firebase console in the account and see my database with no change (small database) (although it seems be slower than normal).
Is there something wrong with firebase Admin SDK? Any help is appreciated.
After spending many hours to find out the cause, I found the problem.
Since I couldn't find any way to enable log of firebase-admin, so it was the dead end to troubleshoot the issue while everything runs silently, so I switched to use the firebase package to have the logging
var firebase = require("firebase");
firebase.initializeApp({
databaseURL: "https://xxxxx.firebaseio.com",
serviceAccount: '....'
});
firebase.database.enableLogging(true); // <=== important
My issue is quite similar to this question
then I could see the actual error:
p:0: Failed to get token: Error: Error refreshing access token:
invalid_grant (Invalid JWT: Token must be a short-lived token and in a
reasonable timeframe)
The solution for this issue was explained on this anwser. This issue caused by a poor synchronisation of the computer's clock where the code was executed that had a lag of 5 minutes (due to a faulty battery for the internal clock).
It started working again when I manually changed the internal time of my computer to the correct one (or totally I reset my computer date-time).
In my case, after resetting the datetime&timezone, the firebase automatically works again and I do not need to re-generate another service account.
In recent days, i have encountered a few problems with my old firebase sdk's in both android and server node js 'firebase' sdk. Sending notifications and retrieving data.
After i updated android dependencies with newest version, notification problem was solved.
But in server side:
Installed firebase admin sdk.
Followed instructions:
https://firebase.google.com/docs/admin/setup
and also checked role of firebase-adminsdk service account.
Problem with the following example:
var admin = require("firebase-admin");
var serviceAccount = require("filepath.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://dbname.firebaseio.com"
});
var db = admin.database();
var ref = db.ref("myval");
ref.once("value",function(snapshot){
console.log(snapshot.val());
});
Example does not return any value or error, just waiting. Sure there is 'myval' child under main database tree.
What wrong may cause the problem?
Solved the problem with help of firebase team.
Enabling the logging before database definition with:
admin.database.enableLogging(true);
var db = admin.database();
helps for debugging. After that I got the error:
Failed to get token: Error: Error fetching access token: invalid_grant (Invalid JWT: Token must be a short-lived token and in a reasonable timeframe)
And according to this:
Authentication on google: OAuth2 keeps returning 'invalid_grant'
my machine timezone was not syncronized appropriately with a ntp server, which is required for new admin authentication in my case.
Then (in ubuntu 14.04) setting the timezone (timezones are here: '$ timedatectl list-timezones') :
$ sudo timedatectl set-timezone desired_timezone
solved the problem!
Thanks for helps.
I have a pretty simple NodeJS server that I'm using to monitor our Firebase Database. My code is basically identical to the sample on the Firebase documentation:
var firebase = require("firebase");
firebase.initializeApp({
databaseURL: 'https://myurl.firebaseio.com/',
serviceAccount: 'path/to/json.json'
})
Now the issue I'm having is when I run this code from within our network, it doens't seem to be connection as a have a block of code right after to read some data and it never gets ran:
var nodeRef = this.db.ref("node");
nodeRef.on("child_added", function (snapshot, prevChildKey) {
// ...
}, function (error) {
console.log(error);
})
If I give everyone write access to the database, I can take out the serviceAccount setting on the initializeApp call, and everything works perfectly. I've tried running Fiddler to see what it might be making a request to that is failing, but I'm not seeing any requests pop up in Fiddler at all. Any ideas what this might be calling that our proxy would need to allow?
Our IT team found what the problem was, I had asked them to open accounts.google.com in our proxy server. It got set to "allow" instead of "tunnel".
According to them, the HSTS headers were causing the SSL decryption on the proxy unless it was set to tunnel, which was causing the "self signed certificate" error I mentioned above in the comments.
For me, disabling Kaspersky got it to work. You can try that.