Search code examples
google-cloud-platformgoogle-cloud-functionscloudevents

Is it possible to trigger cloudEvent with pubsub


Is it possible to trigger cloud event from pubsub.

import { cloudEvent } from "@google-cloud/functions-framework"

export const myCloudEvent = cloudEvent<GoogleDrivePageMessage>("myTopic", cloudEvent => {
  const data = cloudEvent.data;
  logger.log("Called pub sub")
});

and trigger it by calling publishMessage

const pubsub = new PubSub(config);
const topic = pubsub.topic("myTopic");
topic.publishMessage({ data: Buffer.from(messageJson) }, (error, messageId) => {
      if (error) {
        logger.log(`There was an error trying to send pubsub message: ${messageId}`, error)
      }
    });

Also i am trying to test this locally but the emulator doesn't seem to even load cloudEvent function. How would i test this locally without deploying first.


Solution

  • I used onMessagePublished to be able to handle the message

    import { onMessagePublished } from "firebase-functions/v2/pubsub";
    
    export const handleMessage = onMessagePublished<MyMessage>("MYTOPIC", async (event) => {})
    
    

    So it is possible but no with a cloud event i still dont understand the difference.

    Also i would like to add that documentation is not good and there is no support for local development so avoid firebase if you want to build something serious