0

I followed these Algolia/Firebase docs. I did a bit of tweaking as some of the initial setup instructions are outdated but I got it to work. I put all of the code from that page inside my app.js file. Inside terminal I ran 'npm start' and and when I checked my Algolia console the Indices successfully indexed my Firebase data.

app.js

var admin = require("firebase-admin");
admin.initializeApp({
  credential: admin.credential.cert({
    projectId: "*****",
    clientEmail: "*****",
    privateKey: "-----BEGIN PRIVATE KEY-----\n...code...\n-----END PRIVATE KEY-----\n"
  }),
  databaseURL: "*****.firebaseio.com"
});

var firebase = require('firebase');

var config = {
    apiKey: "*****",
    authDomain: "*****.firebaseapp.com",
    databaseURL: "*****.firebaseio.com",
    storageBucket: "*****.appspot.com",
    messagingSenderId: "*****"
  };
  firebase.initializeApp(config);

var algoliasearch = require('algoliasearch');
var client = algoliasearch('*******', '***...**');
var index = client.initIndex('posts');

// Connect to our Firebase contacts data
var rootRef = firebase.database().ref('posts');

// Get all data from Firebase
rootRef.on('value', initIndex);

function initIndex(dataSnapshot) {
  // Array of data to index
  var objectsToIndex = [];
  // Get all objects
  var values = dataSnapshot.val();
  // Process each Firebase ojbect

  for (var key in values) {
    if (values.hasOwnProperty(key)) {
      // Get current Firebase object

      var firebaseObject = values[key];
      // Specify Algolia's objectID using the Firebase object key
      firebaseObject.objectID = key;

      // Add object for indexing
      objectsToIndex.push(firebaseObject);
    }
  }
  // Add or update new objects
  index.saveObjects(objectsToIndex, function(err, content) {
    if (err) {
      throw err;
    }
    console.log('Firebase<>Algolia import done');
  });
}

rootRef.on('value', reindexIndex);
function reindexIndex(dataSnapshot) {
  // Array of objects to index
  var objectsToIndex = [];
  // Create a temp index
  var tempIndexName = 'contacts_temp';
  var tempIndex = client.initIndex(tempIndexName);
  // Get all objects
  var values = dataSnapshot.val();
  // Process each Firebase object
  for (var key in values) {
    if (values.hasOwnProperty(key)) {
      // Get current Firebase object
      var firebaseObject = values[key];
      // Specify Algolia's objectID using the Firebase object key
      firebaseObject.objectID = key;
      // Add object for indexing
      objectsToIndex.push(firebaseObject);
    }
  }
  // Add or update new objects
  index.saveObjects(objectsToIndex, function(err, content) {
    if (err) {
      throw err;
    }
    // Overwrite main index with temp index
    client.moveIndex(tempIndexName, 'contacts', function(err, content) {
      if (err) {
        throw err;
      }
      console.log('Firebase<>Algolia reimport done');
    });
  });
}

// Listen for changes to Firebase data
rootRef.on('child_added', addOrUpdateObject);
rootRef.on('child_changed', addOrUpdateObject);
function addOrUpdateObject(dataSnapshot) {
  // Get Firebase object
  var firebaseObject = dataSnapshot.val();
  // Specify Algolia's objectID using the Firebase object key
  firebaseObject.objectID = dataSnapshot.key;
  // Add or update object
  index.saveObject(firebaseObject, function(err, content) {
    if (err) {
      throw err;
    }
    console.log('Firebase<>Algolia object saved');
  });
}

// Listen for changes to Firebase data
rootRef.on('child_removed', removeIndex);
function removeIndex(dataSnapshot) {
  // Get Algolia's objectID from the Firebase object key
  var objectID = dataSnapshot.key;
  // Remove the object from Algolia
  index.deleteObject(objectID, function(err, content) {
    if (err) {
      throw err;
    }
    console.log('Firebase<>Algolia object deleted');
  });
}

Terminal executed the following statements:

>node app.js

Firebase<>Algolia reimport done
Firebase<>Algolia object saved
Firebase<>Algolia import done
//terminal just sits idle from this point on

As I said the the object was indexed (I only had 1 object inside Firebase).

The problem is after the final Firebase<>Algolia import done statement, nothing happens.

Terminal just sits in a state that seems as if it's waiting for more data to be sent or more instructions to be given. It doesn't do anything. I have to press CTRL+C to exit.

What's the terminal waiting for and why doesn't it exit on its own once it indexes everything from FB?

AL.
  • 36,815
  • 10
  • 142
  • 281
Lance Samaria
  • 17,576
  • 18
  • 108
  • 256

1 Answers1

0

I went to a Meetup and someone pointed out that if I added new data to my Firebase database then it was automatically being indexed inside Algolia. That meant the reason terminal was sitting idle was because it was still listening for events. Basically it's not supposed to stop stay so that it can continuously search.

Lance Samaria
  • 17,576
  • 18
  • 108
  • 256