1
votes

I've created two Firebase functions - one is an HTTPS where I am publishing a message to a topic, and a pub/sub function where I am responding to messages published to that topic.

testPubSub.ts

import { pubsub } from "firebase-functions";
export const testPubSub = pubsub
  .topic("high-scores")
  .onPublish(async (message, context) => {
    console.log("hit test pubsub");
    return null;
  });

testHttps.ts

import { https } from "firebase-functions";
import { messaging } from "firebase-admin";

export const testHookEndpoint = https.onRequest(async (request, response) => {
  const payload = {
    notification: {
      title: "Test title",
      body: "test body"
    }
  };

  const pubsubResponse = await messaging().sendToTopic("high-scores", payload);
  console.log("response from pubsub", pubsubResponse);
  response.send("success");
});

The HTTPS function appears to be running fine (200 response) and messaging is returning a message ID in the response, however I am not seeing the pub/sub function run in the Firebase Console.

When I look at GCP Console I see that "high-scores" has registered as a topic in the Pub/Sub tab, and I'm able to trigger other pub/sub functions in the project through Google Cloud Scheduler.

I'm not sure what step I'm missing for this.

1
so everything is working except for the logging in firebase console?imjared
yeah i'm not getting any errors but that pubsub doesn't appear to running when i look at it either through the firebase console, or through GCP pubsub tab showing activity.eaton9000

1 Answers

4
votes

messaging().sendToTopic("high-scores", payload) is using Firebase Cloud Messaging to send a message to mobile applications subscribed to the given topic. This is completely different than Cloud Pubsub messaging. These two products don't actually have anything in common - FCM is for mobile apps and pubsub is for servers.

What you'll need to do instead is use the node pubsub SDK to send the message to your pubsub topic.