-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubscriber.js
39 lines (33 loc) · 1.27 KB
/
subscriber.js
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
39
require('dotenv').config();
// Imports the Google Cloud client library
const { PubSub } = require('@google-cloud/pubsub');
async function runSubscribe(
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);
}
// Receive callbacks for new messages on the subscription
subscription.on('message', message => {
console.log('Received message:', message.data.toString());
// process.exit(0);
});
// Receive callbacks for errors on the subscription
subscription.on('error', error => {
console.error('Received error:', error);
process.exit(1);
});
}
runSubscribe();