Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

mongo atlas to pub/sub event changes

I want the changes from a collection to pub/sub, for this I am inside the mongo atlas interface, specifically the triggers.

There is a function called changeStream

this way you can send the change, now i'm stuck on how to send it to pubsub.  I am use this code: 

exports = async function(changeEvent) { console.log(JSON.stringify(changeEvent))
const {PubSub} = require('@google-cloud/pubsub');
const {AuthorizationClient} = require('google-auth-library');
// Get the project ID and topic name for Pub/Sub
const projectId = 'xxxx';
const topicName = 'xxxx';
const subscriptionName = 'xxxxx';

// Authenticate with Google Cloud
const auth = new AuthorizationClient();
const authResponse = await auth.authorize();
const headers = {
Authorization: `Bearer ${authResponse.access_token}`,
};

// Get the Pub/Sub client
const pubSubClient = new PubSub({projectId, headers});

// Create or get the subscription
const [subscription] = await pubSubClient.topic(topicName).createSubscription(subscriptionName);

// Listen for messages on the subscription
subscription.on('message', message => {
console.log(`Received message ${message.id}:`);
console.log(`\tData: ${message.data}`);
console.log(`\tAttributes: ${JSON.stringify(message.attributes)}`);
message.ack();
});

// Publish the change event to Pub/Sub
if (!changeEvent) {
console.log('Change event is undefined or null');
return;
}

console.log(`Change event: ${JSON.stringify(changeEvent)}`);

const dataBuffer = Buffer.from(JSON.stringify(changeEvent));
const messageId = await pubSubClient.topic(topicName).publish(dataBuffer);

console.log(`Change event published with message ID: ${messageId}`);
};
Me está arrojando este error:

Error:
{"message":"Value is not an object: undefined","name":"TypeError"}

Can somebody help me? 

My idea is to pass the event changes to a pubsub topic and the use the mongoCDC to bigquery Dataflow template. 

Greetings

 

0 0 639
0 REPLIES 0
Top Labels in this Space