-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpublisher.js
More file actions
38 lines (33 loc) · 1.23 KB
/
publisher.js
File metadata and controls
38 lines (33 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
require('dotenv').config();
// Imports the Google Cloud client library
const { PubSub } = require('@google-cloud/pubsub');
async function runPublish(
projectId = process.env.PROJECT_ID, // Your Google Cloud Platform project ID
topicName = process.env.TOPIC_NAME, // Name for the new topic to create
subscriptionName = process.env.SUBCRIPTION_NAME, // Name for the new subscription to create
keyFilename = process.env.KEY_FILE,
) {
// Instantiates a client
const pubsub = new PubSub({projectId, keyFilename });
let topic = pubsub.topic(topicName);
const [isTopicExists] = await topic.exists();
if(!isTopicExists) {
[topic] = await pubsub.createTopic(topicName);
}
let subscription = topic.subscription(subscriptionName);
const [isSubscriptionExists] = await subscription.exists();
if(!isSubscriptionExists) {
[subscription] = await topic.createSubscription(subscriptionName);
}
// Send a message to the topic
const data = {
id: '4',
label: 'Delta',
// _delete: true, // Flag for action delete document in next tutorial
};
const stringifyData = JSON.stringify(data);
const bufferData = Buffer.from(stringifyData);
await topic.publish(bufferData);
console.log(stringifyData);
}
runPublish();