Topic

Topic

A Topic object allows you to interact with a Cloud Pub/Sub topic.

Constructor

new Topic(pubsub, name, optionsopt)

Parameters:
Name Type Attributes Description
pubsub PubSub

PubSub object.

name string

Name of the topic.

options PublishOptions <optional>

Publisher configuration object.

Examples
```
const {PubSub} = require('@google-cloud/pubsub');
const pubsub = new PubSub();

const topic = pubsub.topic('my-topic');

```
To enable message ordering, set `enableMessageOrdering` to true. Please note that this does not persist to an actual topic.
```
const topic = pubsub.topic('ordered-topic', {enableMessageOrdering: true});
```

Members

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:
See:
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

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

flowControlled() → {FlowControlledPublisher}

Creates a FlowControlledPublisher for this Topic.

FlowControlledPublisher is a helper that lets you control how many messages are simultaneously queued to send, to avoid ballooning memory usage on a low bandwidth connection to Pub/Sub.

Note that it's perfectly fine to create more than one on the same Topic. The actual flow control settings on the Topic will apply across all FlowControlledPublisher objects on that Topic.

Returns:
Type Description
FlowControlledPublisher

The flow control helper.

getPublishOptionDefaults()

Get the default publisher options. These may be modified and passed back into Topic#setPublishOptions.

Example
```
const {PubSub} = require('@google-cloud/pubsub');
const pubsub = new PubSub();

const topic = pubsub.topic('my-topic');

const defaults = topic.getPublishOptionDefaults();
defaults.batching.maxMilliseconds = 10;
topic.setPublishOptions(defaults);
```

resumePublishing(orderingKey)

In the event that the client fails to publish an ordered message, all subsequent publish calls using the same ordering key will fail. Calling this method will disregard the publish failure, allowing the supplied ordering key to be used again in the future.

Parameters:
Name Type Description
orderingKey string

The ordering key in question.

Example
```
const {PubSub} = require('@google-cloud/pubsub');
const pubsub = new PubSub();
const topic = pubsub.topic('my-topic', {messageOrdering: true});

const orderingKey = 'foo';
const data = Buffer.from('Hello, order!');

topic.publishMessage({data, orderingKey}, err => {
  if (err) {
    topic.resumePublishing(orderingKey);
  }
});
```

setPublishOptions(options)

Set the publisher options.

Parameters:
Name Type Description
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(name, optionsopt) → {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.

Parameters:
Name Type Attributes Description
name string

Name of the subscription.

options SubscriberOptions <optional>

Configuration object.

Returns:
Type Description
Subscription
Throws:

If subscription name is omitted.

Type
Error
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.
});
```