node process doesn't exit after firebase once

前端 未结 3 1290
予麋鹿
予麋鹿 2020-11-30 05:54

Using node.js with the npm firebase.

var firebase = require(\'firebase\');
var blahFirebase = new firebase(\'https://myfirebase.firebaseIO.com/blah\');
blahF         


        
相关标签:
3条回答
  • 2020-11-30 06:31

    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();
    });
    
    0 讨论(0)
  • My case is using firebase admin,

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

    and I can end node process by

    return admin.app().delete();
    
    0 讨论(0)
  • 2020-11-30 06:39

    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).

    0 讨论(0)
提交回复
热议问题