One key feature of Firebase is building realtime applications by using their backend to store and sync data. Algolia further enhances that by providing realtime search capabilities. In a few simple steps, this tutorial will teach you how to import your existing data, index new data as it is added to Firebase, and remove indexed data when it is removed from Firebase.
Algolia’s Node.js client simplifies the integration of your Firebase applications with Algolia’s real time search service. The module makes it easy for you to use Algolia’s search capabilities in a manner that will be familiar to those already developing Firebase and Node.js applications.
Prerequisites
Familiar with Firebase
This tutorial assumes you are familiar with Firebase, how it works, and how to build Firebase applications. If you would like to learn more before continuing with this tutorial, I suggest reading the following documentation and tutorials:
Create a Node.js Application
In order to index your Firebase data and continually add/update index information, you will need to create a Node.js application. This application will be responsible for getting data out of Firebase and indexing it with Algolia. It can then be run anywhere like Heroku, Nodejitsu, AWS, Azure, etc.
In this tutorial, we will be indexing contact information in a Firebase application. Be sure to change ‘YourApplicationID’ and ‘YourAPIKey’ to your account values here. Because we are making calls that require more than read access, you will need to create a new key or use an existing one that can Add Records, Delete Records, and Delete Index (for reindexing example). You will also need to set your Firebase ‘INSTANCE’ to the one your application uses.
Here is the intial portion of the Node.js application.
1 2 3 4 5 6 7 |
|
Be sure to install the necessary packages so your application will run.
1 2 |
|
Import Existing Data
In many cases, you may already have data within your Firebase application. In order to integrate with Algolia, you will want to index that data. We will use contact information being stored within Firebaseio as our example. Add the following code to your Node.js application.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
|
To ensure the indexing performs well it is suggested you limit the number of items indexed per call between 1,000 and 10,000 depending on the object size.
Once you run this code, you will have all of your existing Firebase data indexed with Algolia. You will want to remove this code once is is done because the event will continue to fire each time data is added.
Reindex Data
Sometimes, you may have the need to completely reindex your data. This means removing data from the index that may not longer exist, adding new data, and updating existing data. The following code can be added to the Node.js application to perform a reindexing. You will want to remove or comment out the initial index code if currently present.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
|
To ensure the reindexing performs well it is suggested you limit the number of items indexed per call between 1,000 and 10,000 depending on the object size.
Once you run this code, you will have all of your existing Firebase data reindexed with Algolia. You will want to remove this code once is is done because the event will continue to fire each time data is added.
Add or Update Data
Now, we need to handle the case where data is being added or updated. We can easily setup our code to automatically add or update data to our search index by attaching to the ‘child_added’ and ‘child_changed’ events. This will allow us to define code that will be called after data is stored in Firebase. Add the following code to your Node.js application.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
Now, whenever contact data is saved in Firebase, it will automatically be indexed with Algolia.
Delete Data
Next, we need to handle the case where data is deleted from your Firebase application. In order to do this, we can attach to the ‘child_removed’ event. This will allow us to define code that will be called after data is removed from Firebase. Add the following code to your Node.js application.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
Now, whenever contact data is removed from Firebase, it will automatically get removed from Algolia.