title: How to check Firebase connection for each server request? How do I reconnect?
body: <p>I dont know if it is very rudimentary question. I am trying to add Firebase to my web app server side (JAVA), not an android project. My objective is to push updates to the clients for some changes to a particular entity. I am using appengine as the db. So when the entity is updated i need to push the realtime db and the clients(browser) listens for the updated node. I am trying to start off with establishing connection from java. wrote this below code. gets connected to db, but after a few seconds it gets disconnected. </p>
  
  <ol>
  <li>Should i initialize the app again? </li>
  <li>Also once it is connected to Firebase, do i have to check if the connection is still active and the push to Firebase? Does it have to be repeated for every request?</li>
  <li>Is there any better way to write this?</li>
  </ol>
  
  <p>I highly appreciate any help. Successfully implemented with Javascript but struggling with JAVA end. Thanks in advance.</p>
  
  <pre><code>@SuppressWarnings(
          { "unchecked" , "rawtypes" } )
      public static void initiateFirebaseDB() throws FileNotFoundException
          {
  
              if ( FirebaseApp.getApps().isEmpty() )
                  {
  
                      AccessController.doPrivileged( new PrivilegedAction()
                          {
                              public Object run()
                                  {
                                      FileInputStream serviceAccount = null;
                                      try
                                          {
                                              serviceAccount = new FileInputStream( /* path to the json file */  );
                                          }
                                      catch ( FileNotFoundException e )
                                          {
                                              e.printStackTrace();
                                          }
  
                                      FirebaseOptions options = new FirebaseOptions.Builder()
                                              .setCredential( FirebaseCredentials.fromCertificate( serviceAccount ) )
                                              .setDatabaseUrl( /* db url */ ).build();
                                      FirebaseApp.initializeApp( options );
  
                                      return null;
                                  }
                          } );
                  }
  
              DatabaseReference connectedRef = FirebaseDatabase.getInstance().getReference( ".info/connected" );
              connectedRef.addValueEventListener( new ValueEventListener()
                  {
                      @Override
                      public void onDataChange( DataSnapshot snapshot )
                          {
                              boolean connected = snapshot.getValue( Boolean.class );
                              if ( connected )
                                  System.out.println( "connected" );
                              else
                                  {
                                      System.out.println( "not connected" );
                                  }
                          }
  
                      @Override
                      public void onCancelled( DatabaseError error )
                          {
                              System.err.println( "Listener was cancelled" );
                          }
                  } );
          }
  </code></pre>