v1. PublisherClient
The service that an application uses to manipulate topics, and to send messages to a topic.
Constructor
PublisherClient
new PublisherClient(options)
Construct an instance of PublisherClient.
Parameter |
|||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options |
Optional object The configuration object. See the subsequent parameters for more details. Values in
|
Properties
port
The port for this API service.
scopes
The scopes needed to make gRPC calls for every method defined in this service.
servicePath
The DNS address for this API service.
Methods
createTopic
createTopic(request, options, callback) returns Promise
Creates the given topic with the given name. See the resource name rules.
Parameter |
|||||||||
---|---|---|---|---|---|---|---|---|---|
request |
Object The request object that will be sent. Values in
|
||||||||
options |
Optional Object Optional parameters. You can override the default settings for this call, e.g, timeout, retries, paginations, etc. See gax.CallOptions for the details. |
||||||||
callback |
Optional function(nullable Error, nullable Object) The function which will be called with the result of the API call. The second parameter to the callback is an object representing Topic. |
- Returns
-
Promise
- The promise which resolves to an array. The first element of the array is an object representing Topic. The promise has a method named "cancel" which cancels the ongoing API call.
Example
const pubsub = require('@google-cloud/pubsub');
const client = new pubsub.v1.PublisherClient({
// optional auth parameters.
});
const formattedName = client.topicPath('[PROJECT]', '[TOPIC]');
client.createTopic({name: formattedName})
.then(responses => {
const response = responses[0];
// doThingsWith(response)
})
.catch(err => {
console.error(err);
});
deleteTopic
deleteTopic(request, options, callback) returns Promise
Deletes the topic with the given name. Returns NOT_FOUND
if the topic does not exist. After a topic is deleted, a new topic may be created with the same name; this is an entirely new topic with none of the old configuration
or subscriptions. Existing subscriptions to this topic are not deleted, but their topic
field is set to deleted-topic
.
Parameter |
|||||
---|---|---|---|---|---|
request |
Object The request object that will be sent. Values in
|
||||
options |
Optional Object Optional parameters. You can override the default settings for this call, e.g, timeout, retries, paginations, etc. See gax.CallOptions for the details. |
||||
callback |
Optional function(nullable Error) The function which will be called with the result of the API call. |
- Returns
-
Promise
- The promise which resolves when API call finishes. The promise has a method named "cancel" which cancels the ongoing API call.
Example
const pubsub = require('@google-cloud/pubsub');
const client = new pubsub.v1.PublisherClient({
// optional auth parameters.
});
const formattedTopic = client.topicPath('[PROJECT]', '[TOPIC]');
client.deleteTopic({topic: formattedTopic}).catch(err => {
console.error(err);
});
getIamPolicy
getIamPolicy(request, options, callback) returns Promise
Gets the access control policy for a resource. Returns an empty policy if the resource exists and does not have a policy set.
Parameter |
|||||
---|---|---|---|---|---|
request |
Object The request object that will be sent. Values in
|
||||
options |
Optional Object Optional parameters. You can override the default settings for this call, e.g, timeout, retries, paginations, etc. See gax.CallOptions for the details. |
||||
callback |
Optional function(nullable Error, nullable Object) The function which will be called with the result of the API call. The second parameter to the callback is an object representing Policy. |
- Returns
-
Promise
- The promise which resolves to an array. The first element of the array is an object representing Policy. The promise has a method named "cancel" which cancels the ongoing API call.
Example
const pubsub = require('@google-cloud/pubsub');
const client = new pubsub.v1.PublisherClient({
// optional auth parameters.
});
const formattedResource = client.topicPath('[PROJECT]', '[TOPIC]');
client.getIamPolicy({resource: formattedResource})
.then(responses => {
const response = responses[0];
// doThingsWith(response)
})
.catch(err => {
console.error(err);
});
getProjectId
getProjectId(callback)
Return the project ID used by this class.
Parameter |
|
---|---|
callback |
function(Error, string) the callback to be called with the current project Id. |
getTopic
getTopic(request, options, callback) returns Promise
Gets the configuration of a topic.
Parameter |
|||||
---|---|---|---|---|---|
request |
Object The request object that will be sent. Values in
|
||||
options |
Optional Object Optional parameters. You can override the default settings for this call, e.g, timeout, retries, paginations, etc. See gax.CallOptions for the details. |
||||
callback |
Optional function(nullable Error, nullable Object) The function which will be called with the result of the API call. The second parameter to the callback is an object representing Topic. |
- Returns
-
Promise
- The promise which resolves to an array. The first element of the array is an object representing Topic. The promise has a method named "cancel" which cancels the ongoing API call.
Example
const pubsub = require('@google-cloud/pubsub');
const client = new pubsub.v1.PublisherClient({
// optional auth parameters.
});
const formattedTopic = client.topicPath('[PROJECT]', '[TOPIC]');
client.getTopic({topic: formattedTopic})
.then(responses => {
const response = responses[0];
// doThingsWith(response)
})
.catch(err => {
console.error(err);
});
listTopics
listTopics(request, options, callback) returns Promise
Lists matching topics.
Parameter |
|||||||
---|---|---|---|---|---|---|---|
request |
Object The request object that will be sent. Values in
|
||||||
options |
Optional Object Optional parameters. You can override the default settings for this call, e.g, timeout, retries, paginations, etc. See gax.CallOptions for the details. |
||||||
callback |
Optional function(nullable Error, nullable Array, nullable Object, nullable Object) The function which will be called with the result of the API call. The second parameter to the callback is Array of Topic. When autoPaginate: false is specified through options, it contains the result in a single response. If the response indicates the next page exists, the third parameter is set to be used for the next request object. The fourth parameter keeps the raw response object of an object representing ListTopicsResponse. |
- Returns
-
Promise
-
The promise which resolves to an array. The first element of the array is Array of Topic.
When autoPaginate: false is specified through options, the array has three elements. The first element is Array of Topic in a single response. The second element is the next request object if the response indicates the next page exists, or null. The third element is an object representing ListTopicsResponse.
The promise has a method named "cancel" which cancels the ongoing API call.
-
Example
const pubsub = require('@google-cloud/pubsub');
const client = new pubsub.v1.PublisherClient({
// optional auth parameters.
});
// Iterate over all elements.
const formattedProject = client.projectPath('[PROJECT]');
client.listTopics({project: formattedProject})
.then(responses => {
const resources = responses[0];
for (const resource of resources) {
// doThingsWith(resource)
}
})
.catch(err => {
console.error(err);
});
// Or obtain the paged response.
const formattedProject = client.projectPath('[PROJECT]');
const options = {autoPaginate: false};
const callback = responses => {
// The actual resources in a response.
const resources = responses[0];
// The next request if the response shows that there are more responses.
const nextRequest = responses[1];
// The actual response object, if necessary.
// const rawResponse = responses[2];
for (const resource of resources) {
// doThingsWith(resource);
}
if (nextRequest) {
// Fetch the next page.
return client.listTopics(nextRequest, options).then(callback);
}
}
client.listTopics({project: formattedProject}, options)
.then(callback)
.catch(err => {
console.error(err);
});
listTopicsStream
listTopicsStream(request, options) returns Stream
Equivalent to listTopics, but returns a NodeJS Stream object.
This fetches the paged responses for listTopics continuously and invokes the callback registered for 'data' event for each element in the responses.
The returned object has 'end' method when no more elements are required.
autoPaginate option will be ignored.
Parameter |
|||||||
---|---|---|---|---|---|---|---|
request |
Object The request object that will be sent. Values in
|
||||||
options |
Optional Object Optional parameters. You can override the default settings for this call, e.g, timeout, retries, paginations, etc. See gax.CallOptions for the details. |
- See also
- https://nodejs.org/api/stream.html
- Returns
-
Stream
An object stream which emits an object representing Topic on 'data' event.
Example
const pubsub = require('@google-cloud/pubsub');
const client = new pubsub.v1.PublisherClient({
// optional auth parameters.
});
const formattedProject = client.projectPath('[PROJECT]');
client.listTopicsStream({project: formattedProject})
.on('data', element => {
// doThingsWith(element)
}).on('error', err => {
console.log(err);
});
listTopicSubscriptions
listTopicSubscriptions(request, options, callback) returns Promise
Lists the names of the subscriptions on this topic.
Parameter |
|||||||
---|---|---|---|---|---|---|---|
request |
Object The request object that will be sent. Values in
|
||||||
options |
Optional Object Optional parameters. You can override the default settings for this call, e.g, timeout, retries, paginations, etc. See gax.CallOptions for the details. |
||||||
callback |
Optional function(nullable Error, nullable Array, nullable Object, nullable Object) The function which will be called with the result of the API call. The second parameter to the callback is Array of string. When autoPaginate: false is specified through options, it contains the result in a single response. If the response indicates the next page exists, the third parameter is set to be used for the next request object. The fourth parameter keeps the raw response object of an object representing ListTopicSubscriptionsResponse. |
- Returns
-
Promise
-
The promise which resolves to an array. The first element of the array is Array of string.
When autoPaginate: false is specified through options, the array has three elements. The first element is Array of string in a single response. The second element is the next request object if the response indicates the next page exists, or null. The third element is an object representing ListTopicSubscriptionsResponse.
The promise has a method named "cancel" which cancels the ongoing API call.
-
Example
const pubsub = require('@google-cloud/pubsub');
const client = new pubsub.v1.PublisherClient({
// optional auth parameters.
});
// Iterate over all elements.
const formattedTopic = client.topicPath('[PROJECT]', '[TOPIC]');
client.listTopicSubscriptions({topic: formattedTopic})
.then(responses => {
const resources = responses[0];
for (const resource of resources) {
// doThingsWith(resource)
}
})
.catch(err => {
console.error(err);
});
// Or obtain the paged response.
const formattedTopic = client.topicPath('[PROJECT]', '[TOPIC]');
const options = {autoPaginate: false};
const callback = responses => {
// The actual resources in a response.
const resources = responses[0];
// The next request if the response shows that there are more responses.
const nextRequest = responses[1];
// The actual response object, if necessary.
// const rawResponse = responses[2];
for (const resource of resources) {
// doThingsWith(resource);
}
if (nextRequest) {
// Fetch the next page.
return client.listTopicSubscriptions(nextRequest, options).then(callback);
}
}
client.listTopicSubscriptions({topic: formattedTopic}, options)
.then(callback)
.catch(err => {
console.error(err);
});
listTopicSubscriptionsStream
listTopicSubscriptionsStream(request, options) returns Stream
Equivalent to listTopicSubscriptions, but returns a NodeJS Stream object.
This fetches the paged responses for listTopicSubscriptions continuously and invokes the callback registered for 'data' event for each element in the responses.
The returned object has 'end' method when no more elements are required.
autoPaginate option will be ignored.
Parameter |
|||||||
---|---|---|---|---|---|---|---|
request |
Object The request object that will be sent. Values in
|
||||||
options |
Optional Object Optional parameters. You can override the default settings for this call, e.g, timeout, retries, paginations, etc. See gax.CallOptions for the details. |
- See also
- https://nodejs.org/api/stream.html
- Returns
-
Stream
An object stream which emits a string on 'data' event.
Example
const pubsub = require('@google-cloud/pubsub');
const client = new pubsub.v1.PublisherClient({
// optional auth parameters.
});
const formattedTopic = client.topicPath('[PROJECT]', '[TOPIC]');
client.listTopicSubscriptionsStream({topic: formattedTopic})
.on('data', element => {
// doThingsWith(element)
}).on('error', err => {
console.log(err);
});
matchProjectFromProjectName
matchProjectFromProjectName(projectName) returns String
Parse the projectName from a project resource.
Parameter |
|
---|---|
projectName |
String A fully-qualified path representing a project resources. |
- Returns
-
String
- A string representing the project.
matchProjectFromTopicName
matchProjectFromTopicName(topicName) returns String
Parse the topicName from a topic resource.
Parameter |
|
---|---|
topicName |
String A fully-qualified path representing a topic resources. |
- Returns
-
String
- A string representing the project.
matchTopicFromTopicName
matchTopicFromTopicName(topicName) returns String
Parse the topicName from a topic resource.
Parameter |
|
---|---|
topicName |
String A fully-qualified path representing a topic resources. |
- Returns
-
String
- A string representing the topic.
projectPath
projectPath(project) returns String
Return a fully-qualified project resource name string.
Parameter |
|
---|---|
project |
String |
- Returns
-
String
publish
publish(request, options, callback) returns Promise
Adds one or more messages to the topic. Returns NOT_FOUND
if the topic does not exist.
Parameter |
|||||||
---|---|---|---|---|---|---|---|
request |
Object The request object that will be sent. Values in
|
||||||
options |
Optional Object Optional parameters. You can override the default settings for this call, e.g, timeout, retries, paginations, etc. See gax.CallOptions for the details. |
||||||
callback |
Optional function(nullable Error, nullable Object) The function which will be called with the result of the API call. The second parameter to the callback is an object representing PublishResponse. |
- Returns
-
Promise
- The promise which resolves to an array. The first element of the array is an object representing PublishResponse. The promise has a method named "cancel" which cancels the ongoing API call.
Example
const pubsub = require('@google-cloud/pubsub');
const client = new pubsub.v1.PublisherClient({
// optional auth parameters.
});
const formattedTopic = client.topicPath('[PROJECT]', '[TOPIC]');
const data = '';
const messagesElement = {
data: data,
};
const messages = [messagesElement];
const request = {
topic: formattedTopic,
messages: messages,
};
client.publish(request)
.then(responses => {
const response = responses[0];
// doThingsWith(response)
})
.catch(err => {
console.error(err);
});
setIamPolicy
setIamPolicy(request, options, callback) returns Promise
Sets the access control policy on the specified resource. Replaces any existing policy.
Parameter |
|||||||
---|---|---|---|---|---|---|---|
request |
Object The request object that will be sent. Values in
|
||||||
options |
Optional Object Optional parameters. You can override the default settings for this call, e.g, timeout, retries, paginations, etc. See gax.CallOptions for the details. |
||||||
callback |
Optional function(nullable Error, nullable Object) The function which will be called with the result of the API call. The second parameter to the callback is an object representing Policy. |
- Returns
-
Promise
- The promise which resolves to an array. The first element of the array is an object representing Policy. The promise has a method named "cancel" which cancels the ongoing API call.
Example
const pubsub = require('@google-cloud/pubsub');
const client = new pubsub.v1.PublisherClient({
// optional auth parameters.
});
const formattedResource = client.topicPath('[PROJECT]', '[TOPIC]');
const policy = {};
const request = {
resource: formattedResource,
policy: policy,
};
client.setIamPolicy(request)
.then(responses => {
const response = responses[0];
// doThingsWith(response)
})
.catch(err => {
console.error(err);
});
testIamPermissions
testIamPermissions(request, options, callback) returns Promise
Returns permissions that a caller has on the specified resource. If the resource does not exist, this will return an empty set of permissions, not a NOT_FOUND error.
Parameter |
|||||||
---|---|---|---|---|---|---|---|
request |
Object The request object that will be sent. Values in
|
||||||
options |
Optional Object Optional parameters. You can override the default settings for this call, e.g, timeout, retries, paginations, etc. See gax.CallOptions for the details. |
||||||
callback |
Optional function(nullable Error, nullable Object) The function which will be called with the result of the API call. The second parameter to the callback is an object representing TestIamPermissionsResponse. |
- Returns
-
Promise
- The promise which resolves to an array. The first element of the array is an object representing TestIamPermissionsResponse. The promise has a method named "cancel" which cancels the ongoing API call.
Example
const pubsub = require('@google-cloud/pubsub');
const client = new pubsub.v1.PublisherClient({
// optional auth parameters.
});
const formattedResource = client.topicPath('[PROJECT]', '[TOPIC]');
const permissions = [];
const request = {
resource: formattedResource,
permissions: permissions,
};
client.testIamPermissions(request)
.then(responses => {
const response = responses[0];
// doThingsWith(response)
})
.catch(err => {
console.error(err);
});
topicPath
topicPath(project, topic) returns String
Return a fully-qualified topic resource name string.
Parameter |
|
---|---|
project |
String |
topic |
String |
- Returns
-
String
updateTopic
updateTopic(request, options, callback) returns Promise
Updates an existing topic. Note that certain properties of a topic are not modifiable.
Parameter |
|||||||
---|---|---|---|---|---|---|---|
request |
Object The request object that will be sent. Values in
|
||||||
options |
Optional Object Optional parameters. You can override the default settings for this call, e.g, timeout, retries, paginations, etc. See gax.CallOptions for the details. |
||||||
callback |
Optional function(nullable Error, nullable Object) The function which will be called with the result of the API call. The second parameter to the callback is an object representing Topic. |
- Returns
-
Promise
- The promise which resolves to an array. The first element of the array is an object representing Topic. The promise has a method named "cancel" which cancels the ongoing API call.
Example
const pubsub = require('@google-cloud/pubsub');
const client = new pubsub.v1.PublisherClient({
// optional auth parameters.
});
const topic = {};
const updateMask = {};
const request = {
topic: topic,
updateMask: updateMask,
};
client.updateTopic(request)
.then(responses => {
const response = responses[0];
// doThingsWith(response)
})
.catch(err => {
console.error(err);
});