Topic
A Topic object allows you to interact with a Cloud Pub/Sub topic.
Constructor
Topic
new Topic(pubsub, name)
Parameter |
|
---|---|
pubsub |
PubSub object. |
name |
string Name of the topic. |
Example
const {PubSub} = require('@google-cloud/pubsub');
const pubsub = new PubSub();
const topic = pubsub.topic('my-topic');
Properties
iam
IAM (Identity and Access Management) allows you to set permissions on individual resources and offers a wider range of roles: editor, owner, publisher, subscriber, and viewer. This gives you greater flexibility and allows you to set more fine-grained access control.
The IAM access control features described in this document are Beta, including the API methods to get and set IAM policies, and to test IAM permissions. Cloud Pub/Sub's use of IAM features is not covered by any SLA or deprecation policy, and may be subject to backward-incompatible changes.
- Mixes in
- IAM
- See also
Example
const {PubSub} = require('@google-cloud/pubsub');
const pubsub = new PubSub();
const topic = pubsub.topic('my-topic');
//-
// Get the IAM policy for your topic.
//-
topic.iam.getPolicy((err, policy) => {
console.log(policy);
});
//-
// If the callback is omitted, we'll return a Promise.
//-
topic.iam.getPolicy().then((data) => {
const policy = data[0];
const apiResponse = data[1];
});
name
string
The fully qualified name of this topic.
parent
The parent PubSub instance of this topic instance.
pubsub
The parent PubSub instance of this topic instance.
Methods
exists
exists(callback) returns Promise containing TopicExistsResponse
Check if a topic exists.
Parameter |
|
---|---|
callback |
Optional Callback function. |
- Returns
-
Promise containing TopicExistsResponse
Example
const {PubSub} = require('@google-cloud/pubsub');
const pubsub = new PubSub();
const topic = pubsub.topic('my-topic');
topic.exists((err, exists) => {});
//-
// If the callback is omitted, we'll return a Promise.
//-
topic.exists().then((data) => {
const exists = data[0];
});
setPublishOptions
setPublishOptions(options)
Set the publisher options.
Parameter |
|
---|---|
options |
PublishOptions The publisher options. |
Example
const {PubSub} = require('@google-cloud/pubsub');
const pubsub = new PubSub();
const topic = pubsub.topic('my-topic');
topic.setPublishOptions({
batching: {
maxMilliseconds: 10
}
});
subscription
subscription(name, options) returns Subscription
Create a Subscription object. This command by itself will not run any API requests. You will receive a {module:pubsub/subscription} object, which will allow you to interact with a subscription.
Parameter |
|||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
name |
string Name of the subscription. |
||||||||||
options |
Optional object Configuration object. Values in
|
- Throws
-
Error
If subscription name is omitted.
- Returns
Example
const {PubSub} = require('@google-cloud/pubsub');
const pubsub = new PubSub();
const topic = pubsub.topic('my-topic');
const subscription = topic.subscription('my-subscription');
// Register a listener for `message` events.
subscription.on('message', (message) => {
// Called every time a message is received.
// message.id = ID of the message.
// message.ackId = ID used to acknowledge the message receival.
// message.data = Contents of the message.
// message.attributes = Attributes of the message.
// message.publishTime = Timestamp when Pub/Sub received the message.
});