Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use firebase with javafx

am building a desktop application that I want to use the intuitive firebase API for live data syncs. I have been searching the web but no one points out what Jars to use and how to configure with your firebase application. If you can assist please give me steps. I am good at following steps. I want to create a helper class for all firebase operations.

like image 782
Martin Avatar asked May 29 '16 18:05

Martin


People also ask

Can we connect Java with Firebase?

The java support for Firebase is intended to extends its server side functionality by using Firebase's sdk. To query on Firebase realtime database or Firestore, you should write your android/ios app and/o through a web app written on javascript.

Is Firebase REST API?

Firebase REST APIs allow you to make requests to the Firebase Database for reading, writing, updating, or removing data. You can define any Firebase Realtime Database URL as a REST endpoint by adding . json in the end. Keeping your data safe, you can use the Firebase REST API to send requests via an HTTPS client.

Can we create API in Firebase?

The first step in creating an API in Firebase is to access the Firebase console and add a project by clicking “Add project” and giving the new project a name. Google will give you the option to enable Google Analytics for your new project.


2 Answers

It is risky to use the admin SDK on a client machine because it has admin rights to your database. I have developed a work around by creating a WebEngine that will read a html file that has no body just the firebase web scripts.

<!DOCTYPE html>
<html>`
<head>
    <script src="https://www.gstatic.com/firebasejs/4.3.0/firebase.js"></script>

    <title></title>
</head>
<body>
</body>
<script>
    try {
        var config = {
          //firebase config here
        };

        firebase.initializeApp(config);
        var data = firebase.database();
        var ref = data.ref("/ref");
        ref.on("value", function (snapshot) {
            //pass snapshot to java
            alert(JSON.stringify(snapshot.val()));
        });
    } catch (error) {
        alert("Error - jse:" + error.message);
    }
</script>
</html>

and to retrieve it in java

    //Start new webengine to run javascript.
    final WebEngine engine = new WebEngine();
    //get my html file.
    final URL url = NoficationTest.class.getResource("/javafx.html");
    //set to catch alerts from the javascript.
    engine.setOnAlert(event -> {
        try {

            final String data = event.getData();
            if (data != null) {
                if (data.contains("jse")) {
                      //handle if error in javascript.
                } else {
                      //handle if successfull
                }
            }
        } catch (final IOException e) {
            e.printStackTrace();
        }
    });
    //Load html into webengine.
    engine.load(url.toString());

There is a better way for the javascript to call the java side of things, I just did it that way to have a fast answer. I have tested this and it works.

like image 85
Jakob Hartman Avatar answered Sep 18 '22 08:09

Jakob Hartman


Follow the steps from https://firebase.google.com/docs/server/setup

Which can be broken down into:

  • Create a firebase project
  • Download the SDK from Maven
  • Download the service account file which you can create from the web interface
  • Add the SDK
  • Initialize the SDK

I think the problem is that it is easy to overlook some of the steps on the website. Just follow them step by step and read carefully what steps are listed.

like image 34
eikooc Avatar answered Sep 18 '22 08:09

eikooc