firebasefirebase-realtime-databasegoogle-cloud-functionsgoogle-cloud-build

Cannot see the logs from Firebase Database in Google Cloud Run to send push notification


I am trying to use Firebase logs. I am able to add new records into my Firebase Realtime Database Chat entity.
The problem is I cannot see any logs or errors when browsing them at Google Cloud Log explorer.

I submitted succesfully from my folder using this command:

 gcloud builds submit --tag gcr.io/relay-world/realtime-database-handler .

I migrated to artifact registry as suggested and these folders were made:

If I open gcr.io folder I see the realtime-database-handler in there. My goal is to send push notifications to user's devices, but at the moment I am only trying to log the data. Here is code:

const admin = require("firebase-admin");
const { PubSub } = require("@google-cloud/pubsub");

console.log("BEFORE APP INIT");

// Initialize Firebase Admin SDK
admin.initializeApp({
  credential: admin.credential.applicationDefault(),
  databaseURL: "https://relay-world.firebaseio.com", // Replace with your database URL
});

// Initialize Pub/Sub
const pubsub = new PubSub();
const topicName = "realtime-database-chat-topic";

// Reference the Realtime Database path
const db = admin.database();
const ref = db.ref("Chat");

console.log("Listening for Realtime Database changes...");

// Listen for new children added to the collection
ref.on("child_added", async (snapshot) => {
  const newValue = snapshot.val();
  const childId = snapshot.key;

  console.log(`New child added: ${childId}`, newValue);

  // Publish a message to the Pub/Sub topic
  const message = {
    data: {
      id: childId,
      ...newValue,
    },
  };

  try {
    const messageId = await pubsub.topic(topicName).publishMessage({
      json: message,
    });
    console.log(`Message published with ID: ${messageId}`);
  } catch (error) {
    console.error("Error publishing message:", error);
  }
});

Do you know what the problem may be? Many thanks!


Solution

  • Please check my answer here:

    Firebase functions V2: ; SyntaxError: Cannot use import statement outside a module at wrapSafe