Disconnect from Firebase after an initial read i.e. using it as a traditional and not a real-time data store - firebase

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:
How exactly are concurrent users determined for a Firebase app?
Disconnecting a firebase socket without refresh or closing the page
Two questions:
Is the creation of a new Firebase reference via var ref = new Firebase('<url>'); the beginning of a long-polling connection to Firebase?
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?

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.

Related

Firebase custom auth in server-to-server scenario

I need to implement a scenario where, after a file is uploaded to Google Cloud Storage, a function is triggered and processes the file. In this case, processing basically means sanitizing the file, storing it into Firestore and making it accessible via another HTTP-triggered function (a REST API of sorts).
Both user-facing ends of this process (a file upload and HTTP function) need to be secured. The process will be used in server-to-server scenario: one side is going to be a backend written in either Node.js or .NET, the other will be my Firebase solution (Cloud Storage and HTTP-triggered function as per above). In Firebase, I am going to maintain a custom set of users that should have access to the system - my idea was to use a simple system where each user will have a client id and a client secret (basically an oAuth client credentials grant type).
Based on what I read online, an only option to implement this is to use [Firebase auth with custom tokens][1]. I found lots of examples online on how to do that, but it was always about client-to-server scenarios (e.g. a Javascript web app talking to REST API). Server-to-server scenarios were not mentioned anywhere and indeed, I am unsure how to go about implementing it - I can call auth.createCustomToken(uid) just fine in my HTTP Firestore function, but there seem to be no server-side libraries I could use to call auth.SignInWithCustomTokenAsync(customToken).
To sum it up:
How can I use Firebase auth with custom tokens in server-to-server
scenario, where I need to sign in using a previously generated
custom token from a server environment?
If it is not possible,
what's the other alternative to securely implement the
above-described architecture?
I've contacted Google Support and if anyone else is struggling with this, in server-side scenarios, recommended approach is to call signInWithCustomToken endpoint in Firebase Auth REST API.

Firebase JS Issues with Netskope

My company has implemented Netskope for security and it is causing issues with my Firebase web app. I have verified Netskope is the cause of problem by having our security admins disable it on my PC. When that was done, the web app performs as expected. With Netskope enabled, users are able to log in but can't retrieve documents (and I'm assuming can't edit or delete either). Instead, there is an error in the console that says
Could not reach Cloud Firestore backend. Backend didn't respond within 10 seconds. This typically indicates that your device does not have a healthy internet connection at the moment.
Some code:
var app = firebase.initializeApp(config);
var db = firebase.firestore(app);
var docRef = db.collection("/annual meeting/Events/" + selectedDay).orderBy("time").get().then(function(querySnapshot) {
querySnapshot.forEach(function(doc) {
eventIds.push(doc.id);
eventDocs.push(doc.data());
});
addEventsToList();
})
My security people are asking for specific URLs so they can investigate. I've sent them https://console.firestore.google.com/project/*project-name*/firestore, but I'm not sure what else to send them. They seem to think it's either a certificate pinning issue or Firebase not liking Netskope's egress IPs. They need specific URLs to redirect, but I'm not sure what to give them.
According to the Firestore documentation, the client libraries and APIs use the firestore.googleapis.com service to communicate with Firestore.
To call this service, we recommend that you use the Google-provided client libraries. If your application needs to use your own libraries to call this service, use the following information when you make the API requests.
If your security department is requesting URLs to investigate, you can try with the REST endpoints of this API. Inside the documentation, there is an API test app included (API Explorer). This feature lets you make requests to the service with appropriate authentication. For example the following endpoint is used to retrieve documents from Firestore:
https://firestore.googleapis.com/v1/{name=projects/*/databases/*/documents/*/**}
The request parameter name for this endpoint when using the API explorer would be:
projects/projectID/databases/databaseID/documents/documentPath
projectID = your project ID
databaseID = for Firestore use: “(default)”
documentPath = path of the document to retrieve (collection/document/...)
Additionally, I found other questions related to Firebase and Netskope in Stackoverflow and GitHub

Import external data into firestore from provider that pushes data to a websocket you open?

I have a Firestore database which I want to populate with data that is delivered by a live sports event API. This API offers to push the data, so I get new values every time some event happen in a selected game - so we don't have to pull new updates all the time.
However, the delivery method is a websocket which means, that we should open a web socket to a certain endpoint, and then we'll get the data updates.
How could we do this in Firebase? If it was just a simple webhook with an HTTP call, it would be easy to make a firebase functon that could receive the posted data.
But is it possible to do something similar with a web socket? I guess that keeping a Firebase Cloud Function running 24/7 with the web socket is not a good idea at all.
What you're describing is not supported by any Firebase products, and definitely not Cloud Functions. Cloud Functions doesn't support websockets at all, nor does it support any streaming. On top of that, the max lifetime of a function is 9 minutes.
If you absolutely need websockets, consider a different backend infrastructure instead, such as App Engine.

What is the difference between the Firebase REST API and SDK clients? And how to the clients work?

I have a couple of questions on Firebase. I went through their documentation on their site, and the tutorial. I've never used anything like this before, so it's a bit confusing:
I see there is a REST API and a Javascript API. Is the main difference that the REST API is more like a traditional API and requires polling, whereas the Javascript API allows you to receive deltas from Firebase itself?
I want to create a service that receives these deltas and stores them in my own database. But I don't understand how Firebase can keep a connection open for so long. I'm assuming there must be a connection open that Firebase pushes the data through back to my service. Is there a time limit? Or if the connection gets closed is the best practice to detect this error and re-login?
There are many differences between the Firebase REST API and its client libraries. The biggest difference is indeed that most REST clients don't use a persistent connection. But REST clients can listen for changes too, using Firebase's SSE based REST Streaming.
Firebase uses web sockets to establish a persistent connection from the client to the server. On browser platforms where web sockets are not available, the client falls back to HTTP long-polling.

How to use Firebase client to connect to Nest API using multiple client connections (Node.JS client library)?

I'm building a central module that needs to handle multiple users, subscribing them to data changes on their nests.
From what I've been searching, the Node.JS library won't allow me to do multiple firebase connections to the Google Nest API.
Is there any workaround without using REST or REST with streaming?
You can solve this in the Firebase realtime client libraries by creating a new Firebase.Context for each user. This is an undocumented second parameter to the Firebase constructor that may change in future releases, but instructs the instance to set up and maintain a new TCP connection rather than sharing the common one.
An example of its use in Node.JS would be:
var Firebase = require('firebase');
var authToken = 'some_long_auth_token';
var userRef = new Firebase('wss://developer-api.nest.com', new Firebase.Context());
userRef.authWithCustomToken(authToken, function(error) {
// Handle auth error
});
There may well be limitations on how many connections Node.JS will allow you to maintain, but I haven't tested them.

Resources