21

I'm trying to create a simple node.JS command-line script to interact with Firebase using their Javascript API. I want the tool to close the Firebase connection and terminate once it has finished it's interaction.

Here is some sample code showing what I am trying to achieve:

var Firebase = require('firebase');
var myRootRef = new Firebase('https://myprojectname.firebaseIO-demo.com/');
myRootRef.set('It's working!');

One possible solution would be adding a callback and calling process.exit():

var Firebase = require('firebase');
var myRootRef = new Firebase('https://myprojectname.firebaseIO-demo.com/');
myRootRef.set("It's working!", function() { 
    process.exit(0); 
});

However, I would love to have a more elegant solution than forcing the process to terminate with process.exit().

Any ideas?

urish
  • 8,943
  • 8
  • 54
  • 75
  • 1
    There is currently no documented way to terminate a Firebase connection. – Anant Sep 18 '13 at 18:17
  • 1
    Thanks, is there any undocumented way to do so? It's okay even if the code will break in the future... – urish Sep 19 '13 at 10:08
  • 4
    @Anant When is Firebase going to fix this? It would be really nice not to have to use process.exit(0) to terminate a node process in which we have done firebase work. – cayblood Jan 24 '15 at 22:49
  • 1
    this issue is ridiculous indeed; and even more ridiculous is fact that it has not been fixed during the last 3 years. – artur grzesiak Jun 18 '16 at 19:40

3 Answers3

9

i'm not keen on forcing disconnect with process.exit(0) either.. as i only require data persistence (not real-time features) i use the REST API.. I find plain HTTPS twice as fast as the official firebase npm module..

var https = require("https"),
    someData = { some: "data" };

var req = https.request({ 
    hostname: "myprojectname.firebaseio.com", 
    method: "POST", 
    path: "/.json"
});
req.end(JSON.stringify(someData));
Lloyd
  • 8,204
  • 2
  • 38
  • 53
3

This can probably be solved with firebase.database().goOffline(). Got this from another thread: https://stackoverflow.com/a/37858899

Community
  • 1
  • 1
  • This only works in the case of `const db = firebase.database(); db.goOffline();`. Any operation on the db itself causes the process to hang open. – Cody Django Jul 13 '18 at 01:15
2

There are several steps required to properly stop the Firebase JS SDK, so that the Node.js process can terminate normally without forcibly interrupting it with process.exit():

  • You need to cancel any asynchronous listeners (like "on data change" events) that you set up
  • Call firebase.database().goOffline()
  • If you used firebase.auth() you need to call firebase.auth().signOut()
  • Finally, you also need to destroy the Firebase application by calling app.delete()

I've tested this with Node.js version 10 and Firebase JS SDK version 8.

Source: https://blog.famzah.net/2021/01/11/properly-stop-a-firebase-app-in-node-js/

famzah
  • 1,462
  • 18
  • 21