Pub/Sub client libraries

This page shows how to get started with the Cloud Client Libraries for the Pub/Sub API. Client libraries make it easier to access Google Cloud APIs from a supported language. Although you can use Google Cloud APIs directly by making raw requests to the server, client libraries provide simplifications that significantly reduce the amount of code you need to write.

Read more about the Cloud Client Libraries and the older Google API Client Libraries in Client libraries explained.

Install the client library

C++

For information about this client library's requirements and install dependencies, see Setting up a C++ development environment.

C#

Install-Package Google.Cloud.PubSub.V1 -Pre

For more information, see Setting Up a C# Development Environment.

Go

go get cloud.google.com/go/pubsub

For more information, see Setting Up a Go Development Environment.

Java

If you are using Maven, add the following to your pom.xml file. For more information about BOMs, see The Google Cloud Platform Libraries BOM.

<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>libraries-bom</artifactId>
<version>26.71.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
 </dependencies>
</dependencyManagement>
<dependencies>
 <dependency>
 <groupId>com.google.cloud</groupId>
<artifactId>google-cloud-pubsub</artifactId>
</dependency>
</dependencies>

If you are using Gradle, add the following to your dependencies:

implementationplatform('com.google.cloud:libraries-bom:26.72.0')
implementation'com.google.cloud:google-cloud-pubsub'

If you are using sbt, add the following to your dependencies:

libraryDependencies+="com.google.cloud"%"google-cloud-pubsub"%"1.143.1"

If you're using Visual Studio Code or IntelliJ, you can add client libraries to your project using the following IDE plugins:

The plugins provide additional functionality, such as key management for service accounts. Refer to each plugin's documentation for details.

For more information, see Setting Up a Java Development Environment.

Node.js

npm install @google-cloud/pubsub

For more information, see Setting Up a Node.js Development Environment.

PHP

composer require google/cloud-pubsub

For more information, see Using PHP on Google Cloud.

Python

pip install --upgrade google-cloud-pubsub

For more information, see Setting Up a Python Development Environment.

Ruby

gem install google-cloud-pubsub

For more information, see Setting Up a Ruby Development Environment.

Set up authentication

To authenticate calls to Google Cloud APIs, client libraries support Application Default Credentials (ADC); the libraries look for credentials in a set of defined locations and use those credentials to authenticate requests to the API. With ADC, you can make credentials available to your application in a variety of environments, such as local development or production, without needing to modify your application code.

For production environments, the way you set up ADC depends on the service and context. For more information, see Set up Application Default Credentials.

For a local development environment, you can set up ADC with the credentials that are associated with your Google Account:

  1. Install the Google Cloud CLI. After installation, initialize the Google Cloud CLI by running the following command:

    gcloudinit

    If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  2. If you're using a local shell, then create local authentication credentials for your user account:

    gcloudauthapplication-defaultlogin

    You don't need to do this if you're using Cloud Shell.

    If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.

    A sign-in screen appears. After you sign in, your credentials are stored in the local credential file used by ADC.

Use the client library

The following example shows how to use the client library to create a Pub/Sub topic.

C++

namespacepubsub=::google::cloud::pubsub;
namespacepubsub_admin=::google::cloud::pubsub_admin;
[](pubsub_admin::TopicAdminClientclient,std::stringproject_id,
std::stringtopic_id){
autotopic=client.CreateTopic(
pubsub::Topic(std::move(project_id),std::move(topic_id)).FullName());
// Note that kAlreadyExists is a possible error when the library retries.
if(topic.status().code()==google::cloud::StatusCode::kAlreadyExists){
std::cout << "The topic already exists\n";
return;
}
if(!topic)throwstd::move(topic).status();
std::cout << "The topic was successfully created: " << topic->DebugString()
 << "\n";
}

C#


usingGoogle.Cloud.PubSub.V1 ;
usingGrpc.Core ;
usingSystem;
publicclassCreateTopicSample
{
publicTopicCreateTopic(stringprojectId,stringtopicId)
{
PublisherServiceApiClient publisher=PublisherServiceApiClient .Create ();
vartopicName=TopicName .FromProjectTopic (projectId,topicId);
Topic topic=null;
try
{
topic=publisher.CreateTopic (topicName);
Console.WriteLine($"Topic {topic.Name} created.");
}
catch(RpcException e)when(e.Status .StatusCode ==StatusCode .AlreadyExists )
{
Console.WriteLine($"Topic {topicName} already exists.");
}
returntopic;
}
}

Go


// Sample pubsub-quickstart creates a Google Cloud Pub/Sub topic.
packagemain
import(
"context"
"fmt"
"log"
"cloud.google.com/go/pubsub/v2"
"cloud.google.com/go/pubsub/v2/apiv1/pubsubpb"
)
funcmain(){
ctx:=context.Background()
// Sets your Google Cloud Platform project ID.
projectID:="YOUR_PROJECT_ID"
// Creates a client.
client,err:=pubsub.NewClient(ctx,projectID)
iferr!=nil{
log.Fatalf("Failed to create client: %v",err)
}
deferclient.Close()
// Sets the id for the new topic.
topicID:="my-topic"
pbTopic:=&pubsubpb.Topic{
Name:fmt.Sprintf("projects/%s/topics/%s",projectID,topicID),
}
// Creates the new topic.
topic,err:=client.TopicAdminClient.CreateTopic(ctx,pbTopic)
iferr!=nil{
log.Fatalf("Failed to create topic: %v",err)
}
fmt.Printf("Topic %v created.\n",topic)
}

Java


importcom.google.cloud.pubsub.v1.TopicAdminClient ;
importcom.google.pubsub.v1.Topic ;
importcom.google.pubsub.v1.TopicName ;
importjava.io.IOException;
publicclass CreateTopicExample{
publicstaticvoidmain(String...args)throwsException{
// TODO(developer): Replace these variables before running the sample.
StringprojectId="your-project-id";
StringtopicId="your-topic-id";
createTopicExample(projectId,topicId);
}
publicstaticvoidcreateTopicExample(StringprojectId,StringtopicId)throwsIOException{
try(TopicAdminClient topicAdminClient=TopicAdminClient .create()){
TopicName topicName=TopicName .of(projectId,topicId);
Topic topic=topicAdminClient.createTopic(topicName);
System.out.println("Created topic: "+topic.getName ());
}
}
}

Node.js

// Imports the Google Cloud client library
const{PubSub}=require('@google-cloud/pubsub');
asyncfunctionquickstart(
projectId='your-project-id',// Your Google Cloud Platform project ID
topicNameOrId='my-topic',// Name for the new topic to create
subscriptionName='my-sub',// Name for the new subscription to create
){
// Instantiates a client
constpubsub=newPubSub ({projectId});
// Creates a new topic
const[topic]=awaitpubsub.createTopic(topicNameOrId);
console.log(`Topic ${topic.name} created.`);
// Creates a subscription on that new topic
const[subscription]=awaittopic.createSubscription(subscriptionName);
// Receive callbacks for new messages on the subscription
subscription .on ('message',message=>{
console.log('Received message:',message.data.toString());
process.exit(0);
});
// Receive callbacks for errors on the subscription
subscription .on ('error',error=>{
console.error('Received error:',error);
process.exit(1);
});
// Send a message to the topic
awaittopic.publishMessage ({data:Buffer.from ('Test message!')});
}

Python

fromgoogle.cloudimport pubsub_v1
# TODO(developer)
# project_id = "your-project-id"
# topic_id = "your-topic-id"
publisher = pubsub_v1.PublisherClient ()
topic_path = publisher.topic_path(project_id, topic_id)
topic = publisher.create_topic(request={"name": topic_path})
print(f"Created topic: {topic.name}")

Ruby

# Imports the Google Cloud client library
require"google/cloud/pubsub"
# Instantiates a client
pubsub=Google::Cloud::PubSub .new
topic_admin=pubsub.topic_admin
# The name for the new topic
# topic_id = "your-topic-id"
# Creates the new topic
topic=topic_admin.create_topicname:pubsub.topic_path(topic_id)
puts"Topic #{topic.name} created."

Additional resources

C++

The following list contains links to more resources related to the client library for C++:

C#

The following list contains links to more resources related to the client library for C#:

Go

The following list contains links to more resources related to the client library for Go:

Java

The following list contains links to more resources related to the client library for Java:

Node.js

The following list contains links to more resources related to the client library for Node.js:

PHP

The following list contains links to more resources related to the client library for PHP:

Python

The following list contains links to more resources related to the client library for Python:

Ruby

The following list contains links to more resources related to the client library for Ruby:

Additional Pub/Sub API libraries

Language Library
Java java-pubsub-group-kafka-connector

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025年12月05日 UTC.