How to use Hacker News firebase realtime api using the Firebase SDK?

205 Views Asked by At

I'm trying to use the HN firebase api to get realtime updates from HN but none of my event listeners are firing.

I tried to do things like in this post: How can I access the Hacker News API using the Firebase SDK? but couldn't get it to work.

Here's the code that I'm using:

String hnApiCredsFile = "/blah/blah/hn-firebase-adminsdk-foobar.json";
String dbUrl = "https://hacker-news.firebaseio.com";

FileInputStream serviceAccount =
                new FileInputStream(hnApiCredsFile);

FirebaseOptions options = new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(serviceAccount))
.setDatabaseUrl(dbUrl)
.build();

FirebaseApp.initializeApp(options);

final FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference dbRef = database.getReference();
DatabaseReference ref = dbRef.child("v0/user").child("jl");
Thread thread = new Thread(new ShowDbChanges(dbRef));
thread.run();

try {
 System.out.println("About to sleep");
 Thread.sleep(100 * 1000 * 10);
} catch (InterruptedException e) {
 e.printStackTrace();
}

The ShowDbChanges.java class is:

public class ShowDbChanges implements Runnable {

    private DatabaseReference dbRef;

    public ShowDbChanges(DatabaseReference dbRef) {
        this.dbRef = dbRef;
    }

    @Override
    public void run() {
        dbRef.addValueEventListener(new ValueEventListener() {

            public void onDataChange(DataSnapshot dataSnapshot) {
                Object document = dataSnapshot.getValue();
                System.out.println(document);
            }


            public void onCancelled(DatabaseError error) {
                System.out.print("Error: " + error.getMessage());
            }
        });

        dbRef.addListenerForSingleValueEvent(new ValueEventListener() {

            public void onDataChange(DataSnapshot dataSnapshot) {
                Object document = dataSnapshot.getValue();
                System.out.println(document);
            }


            public void onCancelled(DatabaseError error) {
                System.out.print("Error: " + error.getMessage());
            }
        });


    }
}

How do I listen to realtime updates on the firebase SDK for HN?

0

There are 0 best solutions below