I'm using Firebase on the web, with Javascript.
how can I close the connection to the realtime DB ?
var config = {....};
firebase.initializeApp(config);
// get the firebase DB reference
var db = firebase.database().ref();
//doing something
db.push(....);
// close the firebase connection ???
db.close() || firebase.disconnect() || or something like that...
Tried lots of functions - goOffline() , close() , ect.
But nothing does the trick.
Any help would be appreciated... tnx mates
I'm using Firebase on the web, with Javascript.
how can I close the connection to the realtime DB ?
var config = {....};
firebase.initializeApp(config);
// get the firebase DB reference
var db = firebase.database().ref();
//doing something
db.push(....);
// close the firebase connection ???
db.close() || firebase.disconnect() || or something like that...
Tried lots of functions - goOffline() , close() , ect.
But nothing does the trick.
Any help would be appreciated... tnx mates
Share Improve this question asked Oct 21, 2016 at 23:38 Sahar MillisSahar Millis 9072 gold badges14 silver badges23 bronze badges 2 |5 Answers
Reset to default 8Try this-
firebase.initializeApp(config);
// Kill this firebase app.
firebase.app().delete().then(function() {
firebase.initializeApp(newConfig);
});
goOffline() will close connection but Firebase will continue to work locally and the app will remain in memory.
Try using:
firebaseRef.off();
This will end your connection with firebase and stop communicating with the database.
For JSv9, as @Frank van Puffelen said, calling goOffline(db)
Disconnects from the server (all Database operations will be completed offline).
Quoted from Official Document.
Note that the db
here is a Database
instead of a DatabaseRef
.
Maybe you didn't call it in the correct way.
I looked up for this for this jest
error: A worker process has failed to exit gracefully and has been force exited. This is likely caused by tests leaking due to improper teardown. Try running with --detectOpenHandles to find leaks.
(I made a note here in my knowledgebase.) And adding a goOffline(db)
in afterAll()
actually solved it.
For others' information, this is how I disconnect firebase once I fetched needed data.
import {
getDatabase,
goOnline,
goOffline,
get,
child,
ref,
} from 'firebase/database';
// something like this
const db = getDatabase()
try {
goOnline(db);
const snapshot = await get(child(ref(db), `visitors/${id}`));
if (snapshot.exists()) {
const data = snapshot.val();
} else {
console.log('No data available');
}
} catch (error) {
console.log('error', error);
}
goOffline(db);
Seems like you are trying to do this operation server side with CLIENT SIDE conf.
Have you set up correctly the config for you firebase.initializeApp({}) correctly as a server?
Look at the documentation here: https://firebase.google.com/docs/database/server/start#server-sdk-authentication
We had the same issue, but all this disappear when when we correctly set up everything as per the documentation and using it with the "Authenticate with limited privileges" .
var firebase = require("firebase");
// Initialize the app with a service account, granting admin privileges
firebase.initializeApp({
databaseURL: "https://databaseName.firebaseio.com",
serviceAccount: "path/to/serviceAccountCredentials.json"
});
// As an admin, the app has access to read and write all data, regardless of Security Rules
var db = firebase.database();
var ref = db.ref("restricted_access/secret_document");
ref.once("value", function(snapshot) {
console.log(snapshot.val());
});
goOffline()
will close the connection between the client and the database. How did you come to the conclusion that calling it doesn't close the connection? – Frank van Puffelen Commented Oct 21, 2016 at 23:42