Algolia is no longer Syncing with the Firebase database?

I’ve been playing around with this for hours. My iOS app was successfully connecting to Algolia because when there was data inside of Algolia the search results from the iOS app would show that data.

I used Sublime to create my npm folder with my app.js file and everything was successfully synced between Algolia, Heroku, and Firebase. I had my Firebase read/write rules set to true and whenever I ran a search in my iOS any new data was shown.

{
  "rules": {
    ".read": true,
    ".write": true
  }
}

Later on I switched to Visual Studio to add a Firebase databaseAuthVariableOverride to generate a static token that I would use to get authenticated into Firebase:

admin.initializeApp({
  credential: admin.credential.cert({
    projectId: ...,
    privateKey: ...,
    clientEmail: ...
  }),
  databaseURL: ...,
  databaseAuthVariableOverride: {
       uid: "my-service-worker"
    }
});

I changed the Firebase rules to accept the token:

{
  "rules": {
    ".read": "auth.uid != null || auth.uid === 'my-service-worker'",
    ".write": "auth.uid != null"
  }
}

I couldn’t run searches because I kept getting these messages returned from the firebase logs:

c:0:0: Primary connection is healthy. 
p:0: from server: {"r":2,"b":{"s":"permission_denied","d":"Permission denied"}} 
p:0: listen response {"s":"permission_denied","d":"Permission denied"}

Since the token wasn’t working I decided to switch back to the original set up when the rules were to set true and I wasn’t using the databaseAuthVariableOverride.

I switched the rules back.

{
  "rules": {
    ".read": true,
    ".write": true
  }
}

and commented out the databaseAuthVariableOverride in my app.js file.

Now my iOS app will no longer show newly indexed data, it only shows the data that was already sitting inside of Algolia. My app is successfully reaching out to Algolia but Algolia isn’t syncing with the app.js file. The only way Algolia will index new data is if I run npm start in terminal.

Everything was working fine before. Somehow something got corrupted but I can’t figure out what it is. It’s either something in Visual Studio some how messed the app.js file up or when I switched the Firebase rules to the above code and then switched them back to true somehow Algolia stopped syncing up to my app.js file.

I even created a new npm folder from scratch and the problem still exists so I’m flummoxed.

What could be the problem and the solution at this point?

Here is the complete code from my App.js file. I’m using the code from your Algolia Docs but no syncing without running npm start:

const dotenv = require('dotenv');
dotenv.load();

 // Firebase-Admin Initialization
const admin = require("firebase-admin");
admin.database.enableLogging(true); // prints firebase-admin logs
admin.initializeApp({
  credential: admin.credential.cert({
    projectId: process.env.PROJECT_ID,
    clientEmail: process.env.CLIENT_EMAIL,
    privateKey: process.env.PRIVATE_KEY.replace(/\\n/g, '\n')
  }),
  databaseURL: process.env.DATABASE_URL,
  //databaseAuthVariableOverride: { uid: "my-service-worker" }
});

// admin.database.enableLogging(true) needs an active listener to print logs from
var db = admin.database();
var ref = db.ref('sneakers');
ref.once("value", function(snapshot) {
  console.log(snapshot.val());
});

 // Firebase Initialization
const firebase = require('firebase');
firebase.database.enableLogging(true); // prints firebase logs
var config = {
   apiKey: process.env.API_KEY,
   authDomain: process.env.AUTH_DOMAIN,
   databaseURL: process.env.DATABASE_URL,
   storageBucket: process.env.STORAGE_BUCKET,
   messagingSenderId: process.env.MESSENGING_SENDER_ID
};
firebase.initializeApp(config);

// Algolia Initialization
var algoliasearch = require('algoliasearch');
var client = algoliasearch(process.env.ALGOLIA_APP_ID, process.env.ALGOLIA_API_KEY);
var index = client.initIndex(process.env.INDEX_NAME);

// supposed to print algolia logs but doesn't print anything
client.getLogs({
offset: 100,
length: 100,
type: 'error'
}, function(err, content) {
    console.log(err);
    console.log(content);
});

const sneakersRef = firebase.database().ref(process.env.INDEX_NAME);

sneakersRef.on('child_added', addOrUpdateIndexRecord);
sneakersRef.on('child_changed', addOrUpdateIndexRecord);
sneakersRef.on('child_removed', deleteIndexRecord);

function addOrUpdateIndexRecord(contact) {
    // Get Firebase object
    const record = contact.val();
    // Specify Algolia's objectID using the Firebase object key
    record.objectID = contact.key;
    // Add or update object
    index
      .saveObject(record)
      .then(() => {
        console.log('Firebase object indexed in Algolia', record.objectID);
      })
      .catch(error => {
        console.error('Error when indexing contact into Algolia', error);
        process.exit(1);
      });
}

function deleteIndexRecord(contact) {
    // Get Algolia's objectID from the Firebase object key
    const objectID = contact.key;
    // Remove the object from Algolia
    index
      .deleteObject(objectID)
      .then(() => {
        console.log('Firebase object deleted from Algolia', objectID);
      })
      .catch(error => {
        console.error('Error when deleting contact from Algolia', error);
        process.exit(1);
      });
}

My iOS File (this works fine):

let apiClient = Client(appID: "*****", apiKey: "****")
let index = apiClient.index(withName: "sneakers")

Package.json File:

{
  "name": "algolia",
  "version": "1.0.0",
  "description": "Algolia",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node app.js"
  },
  "author": "Lance Samaria",
  "license": "ISC",
  "engines": {
    "node": "7.8.0"
  },
  "dependencies": {
    "algoliasearch": "^3.27.1",
    "dotenv": "^5.0.1",
    "firebase": "^5.0.2",
    "firebase-admin": "^5.12.0",
    "nodejs-latest": "^1.1.0"
  }
}

Procfile:
worker: node ./app.js

I found the answer to my question.

By running heroku logs for some reason my .env constants weren’t getting read after I pushed the new code up to heroku. Once I hardcoded the values everything started working again. Heroku has a feature that allows you to use configuration variables to keep the values hidden in similar fashion to .env variables.

I’ll use those instead.