3

I am trying out Firebase and have seen that one of the limits is the number of concurrent connections. In my use case, I don't actually need real-time anything- I just want to be able to use Firebase as a back-end data store. Like traditional web apps, I would ideally open a connection to Firebase, grab data, then disconnect from Firebase and free up connections for other users.

Various answers here in SO have given me the impression that Firebase makes it difficult to support this kind of usage. See:

  1. How exactly are concurrent users determined for a Firebase app?
  2. Disconnecting a firebase socket without refresh or closing the page

Two questions:

  1. Is the creation of a new Firebase reference via var ref = new Firebase('<url>'); the beginning of a long-polling connection to Firebase?
  2. Is there built-in support/API for creating a short-lived connection to Firebase then being able to disconnect after data retrieval, so that users who idle on the page without doing anything won't eat up my concurrent connection limit?
Community
  • 1
  • 1
Jensen Ching
  • 3,144
  • 4
  • 26
  • 42

1 Answers1

11

Firebase lets you use your URL as a REST endpoint instead of using the JavaScript API.

There is a full tutorial on the Firebase developers site. You can simply use XHR (AJAX) to send and obtain content from Firebase just like you would with any other backend.

The JavaScript API is really powerful for real time apps but in your case, if all you want is stateless transfer, simply making an AJAX request to the RESTful API seems like a much better call.

For example:

var xhr = new XMLHttpRequest();
xhr.open("GET", "https://SampleChat.firebaseIO-demo.com/users/jack/name.json",true);
xhr.onload = function(){
    alert("Got data from my Firebase backend: "+xhr.response);
};
xhr.send();

This should only work in browsers that support CORS since Firebase sends the right headers. You can use something like Angular's $http or jQuery's $.ajax if you want an abstraction layer over native XHR.

Community
  • 1
  • 1
Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504