here the node js code for publishing the message for a topic in cloud pub/sub,
/**
- TODO(developer): Uncomment these variables before running the sample.
*/
// const topicNameOrId = ‘YOUR_TOPIC_NAME_OR_ID’;
// const data = JSON.stringify({foo: ‘bar’});
// Imports the Google Cloud client library
const {PubSub} = require(‘@google-cloud/pubsub’);
// Creates a client; cache this for further use
const pubSubClient = new PubSub();
async function publishMessage() {
// Publishes the message as a string, e.g. “Hello, world!” or JSON.stringify(someObject)
const dataBuffer = Buffer.from(data);
try {
const messageId = await pubSubClient
.topic(topicNameOrId)
.publishMessage({data: dataBuffer});
console.log(Message ${messageId} published.
);
} catch (error) {
console.error(Received error while publishing: ${error.message}
);
process.exitCode = 1;
}
}
publishMessage();
i tried to run this code in my node js work flow it is running successfully but message is not getting posted in pub/sub
here is my whole code
module.exports = defineComponent({
props: {
google_cloud: {
type: “app”,
app: “google_cloud”,
}
},
async run({steps, $}) {
// Required workaround to get the @google-cloud/storage package
// working correctly on Pipedream
require(“@dylburger/umask”)()
const { Storage } = require('@google-cloud/storage')
const key = JSON.parse(this.google_cloud.$auth.key_json)
const storage = new Storage({
projectId: key.project_id,
credentials: {
client_email: key.client_email,
private_key: key.private_key,
}
})
publishMessage();
},
})
const topicNameOrId = ‘projects/demod-362913/topics/TH-to-DarwinBox’;
const data = “hello world”
const {PubSub} = require(‘@google-cloud/pubsub’);
const pubSubClient = new PubSub();
async function publishMessage() {
const dataBuffer =Buffer.from(data);
try {
const messageId = await pubSubClient
.topic(topicNameOrId)
.publishMessage({data: dataBuffer});
console.log(“hi”);
console.log(Message ${messageId} published.
);
} catch (error) {
console.error(Received error while publishing: ${error.message}
);
process.exitCode = 1;
}
}
Can you please resolve this