Constructor

Publisher

new Publisher(topic, options)

A Publisher object allows you to publish messages to a specific topic.

Parameter

topic

Topic

The topic associated with this publisher.

options

Optional

object

Configuration object.

Values in options have the following properties:

Parameter

batching

Optional

object

Batching settings.

batching.maxBytes

Optional

number

The maximum number of bytes to buffer before sending a payload.

batching.maxMessages

Optional

number

The maximum number of messages to buffer before sending a payload.

batching.maxMilliseconds

Optional

number

The maximum duration to wait before sending a payload.

See also

Topics: publish API Documentation

Example

const PubSub = require('@google-cloud/pubsub');
const pubsub = new PubSub();

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

Property

topic

Topic

The topic of this publisher.

Method

publish

publish(data, attributes, callback) returns Promise containing PublisherPublishResponse

Publish the provided message.

Parameter

data

buffer

The message data. This must come in the form of a Buffer object.

attributes

Optional

object

Optional attributes for this message.

callback

Optional

PublisherPublishCallback

Callback function.

Throws

TypeError 

If data is not a Buffer object.

Returns

Promise containing PublisherPublishResponse 

Example

const PubSub = require('@google-cloud/pubsub');
const pubsub = new PubSub();

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

const data = Buffer.from('Hello, world!');

const callback = function(err, messageId) {
  if (err) {
    // Error handling omitted.
  }
};

publisher.publish(data, callback);

//-
// Optionally you can provide an object containing attributes for the
// message.
//-
const attributes = {
  key: 'value'
};

publisher.publish(data, attributes, callback);

//-
// If the callback is omitted, we'll return a Promise.
//-
publisher.publish(data).then(function(messageId) {});