Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Closing a firebase connection in Node.JS

Tags:

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?

like image 316
urish Avatar asked Sep 18 '13 11:09

urish


People also ask

How do I close firebase?

Try using: firebaseRef. off(); This will end your connection with firebase and stop communicating with the database.

How do I retrieve data from Firebase realtime database in node JS?

Firebase data is retrieved by either a one time call to GetValueAsync() or attaching to an event on a FirebaseDatabase reference. The event listener is called once for the initial state of the data and again anytime the data changes.

What is the use of firebase in Nodejs?

Overview. Firebase provides the tools and infrastructure you need to develop, grow, and earn money from your app. This package supports web (browser), mobile-web, and server (Node. js) clients.


1 Answers

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));
like image 132
Lloyd Avatar answered Oct 18 '22 17:10

Lloyd