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.

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