如何基于事件Google PubSub发送消息?



我需要一些帮助与PubSub…

每次有人在我的网站上接受cookie时,我需要向主题发送消息。此消息应包含包含接受的服务的encodedURL。

我有这个脚本:

const topicName = "myTopic";
const data = JSON.stringify({
encodeConsentURL:""
});
// 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 publishMessageWithCustomAttributes() {
// Publishes the message as a string, e.g. "Hello, world!" or JSON.stringify(someObject)
const dataBuffer = Buffer.from(data);
// Add two custom attributes, origin and username, to the message
const customAttributes = {};
const messageId = await pubSubClient
.topic(topicName)
.publish(dataBuffer, customAttributes);
console.log(`Message ${messageId} published.`);
console.log(customAttributes);
}
publishMessageWithCustomAttributes().catch(console.error);

这段代码工作,它发送消息,我发现很难做的是如何设置一切正确在我的cookie脚本中运行这段代码。在我的cookie脚本中,我有一个函数,写cookie,在同一个函数中,我想发送消息,这是可能的吗?提前感谢!

这是有点晚了,但我不明白,如果你已经有一个工作的cookie脚本和一个工作的发布脚本,是不是只是把它们放在一起?如果你还需要帮助,我很乐意帮助你。

之类的
const runEverything = async () => {
try {
await checkCookiesThing()
await publishMessage()
} catch (e) {
console.error(e)
}
}

最新更新