Constructor

Publisher

new Publisher(topic, options)

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. Defaults to 100 milliseconds.

gaxOpts

Optional

object

Request configuration options, outlined here: https://googleapis.github.io/gax-nodejs/CallSettings.html.

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 with string properties

Attributes for this message.

callback

Optional

PublisherPublishCallback

Callback function.

Throws

TypeError 

If data is not a Buffer object.

TypeError 

If any value in attributes object is not a string.

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 = (err, messageId) => {
  if (err) {
    // Error handling omitted.
  }
};

publisher.publish(data, callback);

//-
// Optionally you can provide an object containing attributes for the
// message. Note that all values in the object must be strings.
//-
const attributes = {
  key: 'value'
};

publisher.publish(data, attributes, callback);

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