1

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?

J. Cool
  • 75
  • 3
  • 7
  • @smitop I see that you helped solve: https://stackoverflow.com/questions/69518903/how-can-i-access-the-hacker-news-api-using-the-firebase-sdk. Do you have any idea how I could fix my similar issue? – J. Cool Jun 15 '23 at 16:12

0 Answers0