25

Using node.js with the npm firebase.

var firebase = require('firebase');
var blahFirebase = new firebase('https://myfirebase.firebaseIO.com/blah');
blahFirebase.once('value', function (snapshot) {
    //
});

Why does node not exit when it is done reading the data?

Tomas Re
  • 349
  • 3
  • 7

4 Answers4

25

In the new Firebase API you should use firebase.app.App.delete() to free the resources its holding. For example:

var app = firebase.initializeApp({ ... });
var db = firebase.database();

// Do something

app.delete(); // Release resources

Do not use process.exit() since it will stop the entire process (which is not what you would usually want).

locker47
  • 259
  • 1
  • 3
  • 4
  • 2
    You can also use `var firebase = require("firebase") firebase.app('[DEFAULT]').delete()` – Adam Loving Nov 01 '16 at 05:18
  • But how I can stop to listening an ```.on('value', function () {})```? I have a search form, and I have to stop the listening to start another with different parameters. – JCarlosR Nov 01 '16 at 16:09
  • 2
    Using ```app.delete()``` doesn't exit my process after signing in and signing out. Any luck with this? Here is my minimal example: https://gist.github.com/juriejan/2134fc2569e56a24fc9b886f1f156198 – juriejan Apr 20 '17 at 07:22
  • If you used `firebase.auth()`, you may need to log out first, too: https://stackoverflow.com/a/65660764/198219 – famzah Jan 11 '21 at 02:51
13

My case is using firebase admin,

const  admin = require('firebase-admin');

and I can end node process by

return admin.app().delete();
Jake
  • 12,713
  • 18
  • 66
  • 96
William Wu
  • 473
  • 5
  • 16
  • If it doesn't work, try changing when you're placing it this line of code (towards the end) – Max Jul 01 '20 at 13:06
11

Update

Note that this is no longer applicable. Node.js will no longer hang when using once(), although it will be held open as long as there are active listeners subscribed to the remote server.

Original

The Firebase process opens sockets to the server and establishes listeners for incoming data on those connections. Just like a node web server, awaiting incoming HTTP connections, this holds the process open.

To end the process, you can simply utilize process.exit() from inside the callback:

blahFirebase.once('value', function (snapshot) {
    //
    process.exit();
});
Kato
  • 40,352
  • 6
  • 119
  • 149
  • 3
    I am using Node.js and Firebase to develop an AWS lambda. Call lambda would fail if I call process.exit(), but if I do nothing, it will timeout. database.goOffline() doesn't work. Any better solution? – syshen Aug 03 '16 at 08:05
  • 1
    @syshen in Lambda you should call `context.succeed()` instead. – idbehold Oct 05 '16 at 22:47
  • with `process.exit()` the process ends but I don't get data written to the database. Have anyone ran into this problem? – grigy May 23 '17 at 01:37
  • use firebase.delete() instead. https://firebase.google.com/docs/reference/node/firebase.app.App#delete – pantos27 Jun 13 '17 at 20:58
  • Calling firebase.database().goOffline() or firebase.app().delete(). https://firebase.google.com/support/release-notes/js – pigfly Mar 15 '18 at 23:21
-1
setTimeout(()=> {
    process.exit(1);
}, 1000);

Dunno why, but solved the problem.

red23jordan
  • 2,841
  • 10
  • 41
  • 57