Constructor

Topic

new Topic(pubsub, name)

Parameter

pubsub

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

Access Control Overview

What is Cloud IAM?

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

PubSub

The parent PubSub instance of this topic instance.

pubsub

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

TopicExistsCallback

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 options have the following properties:

Parameter

flowControl

Optional

object

Flow control configurations for receiving messages. Note that these options do not persist across subscription instances.

flowControl.maxBytes

Optional

number

The maximum number of bytes in un-acked messages to allow before the subscription pauses incoming messages. Defaults to 20% of free memory.

flowControl.maxMessages

Optional

number

The maximum number of un-acked messages to allow before the subscription pauses incoming messages.

maxConnections

Optional

number

Use this to limit the number of connections to be used when sending and receiving messages.

Throws

Error 

If subscription name is omitted.

Returns

Subscription 

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