Auto refreshing your Firebase app using Pub/Sub and Cloud Functions

Get data for any location

Start your search

Fresh or stale data?

Here at Unacast we have spent the last 6 months making a self-service data delivery management system for our Sales and Client Success people. This has become a necessity because of our skyrocketing data volumes and the steady increase of both data samples and client integrations. The front end solution we have implemented to solve this problem is only one part of a much larger microservice puzzle, where several independent apps handle the different steps in the data delivery pipeline. So how do we make the front reflect the changes in states (both successes and errors) that happens as the data is passing through the pipe?

We released a first version where this was handled in the easiest way thinkable, with a refresh button like this. This was not optimal in any way, as it raised a lot of "why hasn't this order been executed for the last 3 days" questions when people forgot to press the button to get to an updated state. It soon became clear that we had to fix this and make this refresh automatic, so we tested another simple solution; triggering the refresh function on an interval. With an increasing number of users, an increasing number of orders and shorter intervals between each order execution, this wasn't viable either.

So then we decided to do this the proper way; to refresh the UI only when the downstream systems had altered its state or an error had occurred.

How Does The Data Flow?

Here's a simplified diagram showing the different parts involved, how they communicate, and how the data flows.

Flow of data and events

As you can see it is all Firebase and Google Cloud components, and I'm now going to explain a bit more what happens here:

Firebase

The frontend is a React app hosted on Firebase and it uses a Firestore NoSQL database to store specifications of what we call Orders. An Order is a specification of what data should be shipped where, and at what interval.

Backend

When an Order is registered it is posted to a backend consisting of several services running on Google Kubernetes Engine. From this point on the Order is handled and executed by these systems. The backend uses a Cloud SQL database to store Events tied to the Order, along with the Order itself, as it alters state on its way through the pipeline. It is these Events that we want to trigger an automatic refresh in the Web frontend, so the user that issued the Order can follow the progress of the Order.

Pub/Sub

Every time the data delivery pipeline changes the state of an order, or an error occurs, a message is sent on a Topic on Pub/Sub. This way any system that is interested in reacting to a state change in an Order can have its own Subscription and receive messages with relevant data about these Events.

Cloud Functions

For our particular use case the subscriber to the State Change Topic is a Cloud Function, the Firebase variant, that leaps into action and grabs the Order's ID from the message body. It then uses the ID to fetch an up to date representation of the Order from the Cloud SQL database and uses this to update the Frontend's representation of the order in Firestore.

Firestore -> Firebase sync

Finally we arrive at the really neat part of this whole setup. The React components in our Frontend, that are coupled to and represent the data stored in documents in the Firestore database, will automatically be refreshed when that document is updated. No need for that pesky refresh button anymore!

Demo Time

Here is a short demo of how this looks in action, using a dummy Order that is scheduled to trigger every two minutes. As you can see we can follow it through all oif its states in real time from its creation to completion. No more wondering whether the Order you look at is up to date and showing its most current state.

 

Frequently Asked Questions

Discover how analyzing real-world movement patterns can reveal valuable trends in customer behavior, optimize business operations, and enhance strategic decision-making.

What is site selection and why is it important?

Site selection is the strategic process by which businesses identify, evaluate, and choose optimal locations for their operations. This process is paramount as the location of a business directly influences factors such as accessibility, visibility, profitability, and market longevity. For retailers, the right site can mean higher customer footfall and increased sales. In real estate, a well-selected site can promise lucrative returns on investment and tenant stability. Financial service firms leverage site selection to position their branches or ATMs in high-demand areas. Essentially, site selection plays a pivotal role in ensuring the success and growth of a business by aligning its physical presence with market opportunities and demands.

How does location intelligence enhance site selection?

Location intelligence refers to the harnessing of geospatial data to derive actionable insights, which can significantly enhance the site selection process. By analyzing data like consumer demographics, foot traffic patterns, competitor locations, trade area data, and more, businesses can make more informed decisions about where to establish or expand their operations. Location intelligence allows for a deeper understanding of market dynamics, revealing hidden opportunities or potential pitfalls. For instance, retailers can identify gaps in the market, real estate professionals can forecast property value trends, and financial service providers can assess areas with high customer demand. Advanced tools, like those offered by Unacast, further refine these insights by leveraging AI and machine learning, enabling more precise and timely decision-making.

What challenges do businesses face in the site selection process?

Unacast provides invaluable support to businesses during the site selection process through its advanced location data and analytics software, all powered and refined by Artificial Intelligence and Machine Learning technologies. The company offers a suite of products designed to deliver accurate, actionable, and comprehensive location intelligence. This data proves crucial for businesses looking to understand consumer behavior, analyze traffic patterns, evaluate competitor locations, and much more. With Unacast’s robust tools, businesses in retail, real estate, and financial services can derive insightful information necessary for making strategic, informed site selection decisions. The platform not only provides reliable data but also ensures it is readily actionable for businesses, whether they are looking to open a new store, invest in property, or expand their financial services to new locations.

What types of location data are crucial for informed site selection?

Demographic data offers insights into the age, income, and lifestyle of people in a particular area, helping businesses understand their potential customer base. Foot traffic data provides information on the number of people visiting a location, which is crucial for retailers to estimate the store's potential popularity and for real estate professionals to assess an area's vibrancy and demand. Geographic Information System (GIS) data helps in visualizing and analyzing geographical details, supporting companies in identifying accessible and strategically located sites. Understanding the proximity to competitors, accessibility, and the socio-economic profile of the surrounding areas is also vital. Unacast’s platform aggregates and analyzes these various data types, providing a holistic view that significantly empowers businesses in their site selection endeavors.

Resources

Sort
No items found.

Book a Meeting

Meet with us and put Unacast’s data to the test.
bird's eye view of the city