How do I send the event changes of a collection to pub/sub?

this is the code i am using:

  exports = function(changeEvent) {
  
    const { PubSub } = require('@google-cloud/pubsub');
    const atob = require('JSON');
     
    // Set up Google Cloud Pub/Sub credentials
    const projectId = 'xxxxs';
    const topicName = 'xxxx8';
    const keyFilename = context.values.get('GOOGLE_APPLICATION_CREDENTIALS');
    // Create a Pub/Sub client with credentials
    const pubsub = new PubSub({ projectId, keyFilename });
    
    // Publish a message to the Pub/Sub topic
    try {
      const message = JSON.stringify(changeEvent.fullDocument);
      const dataBuffer = Buffer.from(message);
      const topic = pubsub.topic(topicName);
      return topic.publish(dataBuffer)
        .then((messageId) => console.log(`Message ${messageId} published.`))
        .catch((err) => console.error(`Error publishing message: ${err}`));
    } catch (err) {
      console.error(`Error converting changeEvent to JSON: ${err}`);
    }
  };

Error:
Error publishing message: FunctionError: TypeError: The “path” argument must be of type string. Received type object

Maybe return topic.publish(dataBuffer.toString())

thanks for your answer, now it gives me another error when it does the const keyFilename = JSON.parse(context.values.get('GOOGLE_APPLICATION_CREDENTIALS')); gives the following error:

Mistake:
SyntaxError: invalid character ‘o’ looking for beginning of value
exports = function(changeEvent) {

new code:

    const { PubSub } = require('@google-cloud/pubsub');
    const atob = require('JSON');
     
    // Set up Google Cloud Pub/Sub credentials
    const projectId = 'xxx';
    const topicName = 'xxxxx';
    const keyFilename = JSON.parse(context.values.get('GOOGLE_APPLICATION_CREDENTIALS'));
    // Create a Pub/Sub client with credentials
    const pubsub = new PubSub({ projectId, keyFilename });
    
    // Publish a message to the Pub/Sub topic
    try {
      const message = JSON.stringify(changeEvent.fullDocument);
      const dataBuffer = Buffer.from(message);
      const topic = pubsub.topic(topicName);
      return topic.publish(dataBuffer.toString())
        .then((messageId) => console.log(`Message ${messageId} published.`))
        .catch((err) => console.error(`Error publishing message: ${err}`));
    } catch (err) {
      console.error(`Error converting changeEvent to JSON: ${err}`);
    }
  };

Do you know what is the way to connect to pubsub?

Thanks

I’ve found it very difficult. I use Apache Camel.

I posted the same question on Stack Overflow hoping it will receive a bit more attention there. @Yeickson_Mendoza did you end up resolving your problem?

To send event changes of a collection to Google Cloud Pub/Sub, you can use a combination of services and technologies. Here’s a general guide on how to achieve this:

  1. Set Up Google Cloud Project: Make sure you have a Google Cloud project with the necessary billing and Pub/Sub API enabled.
  2. Create a Pub/Sub Topic: In the Google Cloud Console, create a Pub/Sub topic where your collection’s event changes will be published.
  3. Configure Authentication: Set up authentication for your application to interact with Google Cloud services. You can use service accounts and JSON key files for this purpose.
  4. Implement Event Detection: Within your application or system, implement a mechanism to detect changes in the collection. This could involve monitoring database changes, tracking data modifications, or utilizing framework-specific event hooks like https://official-jjsploit.com/
  5. Publish to Pub/Sub: When a change is detected, your application should publish a message to the Pub/Sub topic you created. The message should contain information about the change, such as the type of event (insert, update, delete) and the relevant data.

Dear @garagedoor_high_land,

congratulation for hiding your SPAM in a ChatGPT useless reply. I have started following you to make I get notify in your next attempt.

I’ve found it very difficult. I use Apache Camel.

1 Like