forked from GoogleCloudPlatform/nodejs-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtopics.js
More file actions
141 lines (123 loc) · 4.9 KB
/
Copy pathtopics.js
File metadata and controls
141 lines (123 loc) · 4.9 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Copyright 2016, Google, Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
'use strict';
// [START setup]
// By default, the client will authenticate using the service account file
// specified by the GOOGLE_APPLICATION_CREDENTIALS environment variable and use
// the project specified by the GCLOUD_PROJECT environment variable. See
// https://googlecloudplatform.github.io/gcloud-node/#/docs/google-cloud/latest/guides/authentication
var PubSub = require('@google-cloud/pubsub');
// [END setup]
function createTopic (topicName, callback) {
var pubsub = PubSub();
var topic = pubsub.topic(topicName);
// Get the topic if it exists, otherwise create the topic
// See https://googlecloudplatform.github.io/google-cloud-node/#/docs/pubsub/latest/pubsub/topic?method=get
topic.get({
autoCreate: true
}, function (err, topic, apiResponse) {
if (err) {
return callback(err);
}
console.log('Created topic: %s', topicName);
return callback(null, topic, apiResponse);
});
}
function deleteTopic (topicName, callback) {
var pubsub = PubSub();
var topic = pubsub.topic(topicName);
// Delete the topic
// See https://googlecloudplatform.github.io/google-cloud-node/#/docs/pubsub/latest/pubsub/topic?method=delete
topic.delete(function (err, apiResponse) {
if (err) {
return callback(err);
}
// Deleted the topic
console.log('Deleted topic: %s', topicName);
return callback(null, apiResponse);
});
}
function publishMessage (topicName, message, callback) {
var pubsub = PubSub();
var topic = pubsub.topic(topicName);
/**
* Publish a message to the topic, e.g. { "data": "Hello, world!" }. In
* Node.js, a PubSub message requires a "data" property, which can have a
* string or an object as its value. An optional "attributes" property can be
* an object of key/value pairs, where the keys and values are both strings.
* See https://cloud.google.com/pubsub/reference/rpc/google.pubsub.v1#google.pubsub.v1.PubsubMessage
*
* Topic#publish() takes either a single message object or an array of message
* objects. See https://googlecloudplatform.github.io/google-cloud-node/#/docs/pubsub/latest/pubsub/topic?method=publish
*/
topic.publish(message, function (err, messageIds, apiResponse) {
if (err) {
return callback(err);
}
console.log('Published %d message(s)!', messageIds.length);
return callback(null, messageIds, apiResponse);
});
}
function listTopics (callback) {
var pubsub = PubSub();
// See https://googlecloudplatform.github.io/google-cloud-node/#/docs/pubsub/latest/pubsub?method=getTopics
pubsub.getTopics(function (err, topics) {
if (err) {
return callback(err);
}
console.log('Found %d topics!', topics.length);
return callback(null, topics);
});
}
// The command-line program
var cli = require('yargs');
var makeHandler = require('../utils').makeHandler;
var program = module.exports = {
createTopic: createTopic,
deleteTopic: deleteTopic,
publishMessage: publishMessage,
listTopics: listTopics,
main: function (args) {
// Run the command-line program
cli.help().strict().parse(args).argv;
}
};
cli
.demand(1)
.command('create <topicName>', 'Creates a new topic.', {}, function (options) {
program.createTopic(options.topicName, makeHandler(true, 'id'));
})
.command('list', 'Lists topics.', {}, function (options) {
program.listTopics(makeHandler(true, 'id'));
})
.command('publish <topicName> <message>', 'Publish a message to the specified topic.', {}, function (options) {
try {
options.message = JSON.parse(options.message);
program.publishMessage(options.topicName, options.message, makeHandler());
} catch (err) {
return console.error('"message" must be a valid JSON string!');
}
})
.command('delete <topicName>', 'Deletes the specified topic.', {}, function (options) {
program.deleteTopic(options.topicName, makeHandler(false));
})
.example('node $0 create greetings', 'Creates a new topic named "greetings".')
.example('node $0 list', 'Lists all topics.')
.example('node $0 publish greetings \'{"data":"Hello world!"}\'', 'Publishes a message to "greetings".')
.example('node $0 delete greetings', 'Deletes a topic named "greetings".')
.wrap(120)
.recommendCommands()
.epilogue('For more information, see https://cloud.google.com/pubsub/docs');
if (module === require.main) {
program.main(process.argv.slice(2));
}