Search code examples
postgresqlfirebasegoogle-cloud-firestoregoogle-cloud-sqlgoogle-cloud-pubsub

Is it possible to publish a message to Google Pub/Sub whenever a data is inserted or updated in Google Cloud SQL?


I'm new to Google Cloud SQL and Pub/Sub. I couldn't find documentation anywhere about this. But another question's accepted and upvoted answer seems to say it is possible to publish a Pub/Sub message whenever there is an insert happen to the database. Excerpt from that answer:

2 - The ideal solution would be to create the Pub/Sub topic and publish to it when you insert new data to the database.

But since my question is a different one, thus I asked a new question here.

Background: I'm using a combination of Google Cloud SQL, Firestore and Realtime Database for my app for its own unique strengths.

What I want to do is to be able to write into Firestore and Realtime databases once an insert is successful in Google Cloud SQL. According to the answer above, this is the steps I should do:

  1. The app calls a Cloud Function to insert a data into Google Cloud SQL database (PostgreSQL). Note: The Postgres tables has some important constraints and triggers Postgres functions, thats why we want to start here.
  2. When the insert is successful I want Google Cloud SQL to publish a message to Pub/Sub.
  3. Then there is another Cloud Function that subscribes to the Pub/Sub topic. This function will write into Firestore / Realtime Database accordingly.

I got steps #1 & #3 all figured out. The solution I'm looking for is for step #2.


Solution

  • The answer in the other question is simply suggesting that your code do both of the following:

    1. Write to Cloud SQL.
    2. If the write is successful, send a message to a pubsub topic.

    There isn't anything that will automate or simplify either of these tasks. There are no triggers for Cloud Functions that will respond to writes to Cloud SQL. You write code for task 1, then write the code for task 2. Both of these things should be straightforward and covered in product documentation. I suggest making an attempt at both (separately), and posting again with the code you have that isn't working the way you expect.

    If you need to get started with pubsub, there are SDKs for pretty much every major server platform, and the documentation for sending a message is here.