Publish and receive messages in Pub/Sub by using the client libraries
This page shows you how to do the following:
Create a project, enable billing, and enable the Pub/Sub Lite API using the Google Cloud CLI.
Create Lite reservations, Lite topics, and Lite subscriptions using the Google Cloud CLI.
Send and receive messages using the Pub/Sub Lite client library for Go, Java, and Python.
Before you begin
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
Create or select a Google Cloud project.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_ID
with a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_ID
with your Google Cloud project name.
-
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Pub/Sub Lite API:
gcloud services enable pubsublite.googleapis.com
-
Create local authentication credentials for your user account:
gcloud auth application-default login
-
Grant roles to your user account. Run the following command once for each of the following IAM roles:
roles/pubsublite.admin
gcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE
- Replace
PROJECT_ID
with your project ID. -
Replace
USER_IDENTIFIER
with the identifier for your user account. For example,user:myemail@example.com
. - Replace
ROLE
with each individual role.
- Replace
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
Create or select a Google Cloud project.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_ID
with a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_ID
with your Google Cloud project name.
-
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Pub/Sub Lite API:
gcloud services enable pubsublite.googleapis.com
-
Create local authentication credentials for your user account:
gcloud auth application-default login
-
Grant roles to your user account. Run the following command once for each of the following IAM roles:
roles/pubsublite.admin
gcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE
- Replace
PROJECT_ID
with your project ID. -
Replace
USER_IDENTIFIER
with the identifier for your user account. For example,user:myemail@example.com
. - Replace
ROLE
with each individual role.
- Replace
Install the client library
Go
go get cloud.google.com/go/pubsublite
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.
If you are using Gradle, add the following to your dependencies:
If you are using sbt, add the following to your dependencies:
If you're using Visual Studio Code, IntelliJ, or Eclipse, 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.
Python
pip install --upgrade google-cloud-pubsublite
Create a Lite reservation
To create a Lite reservation, use the
gcloud pubsub lite-reservations create
command. The following sample creates a reservation called testRes
.
gcloud pubsub lite-reservations create testRes \ --location=us-central1 \ --throughput-capacity=1
Create a regional Lite topic
To create a regional Lite topic, use the gcloud pubsub lite-topics create
command. The following sample creates a topic called testTopic
.
gcloud pubsub lite-topics create testTopic \ --location=us-central1 \ --partitions=1 \ --per-partition-bytes=30GiB \ --throughput-reservation=projects/PROJECT_ID/locations/us-central1/reservations/testRes \
Replace the following:
PROJECT_ID: the ID of the project
Create a Lite subscription
To create a Lite subscription, use the
gcloud pubsub lite-subscriptions create
command. The following sample creates a subscription called testSub
.
gcloud pubsub lite-subscriptions create testSub \ --location=us-central1 \ --topic=testTopic \ --delivery-requirement=deliver-immediately
Send messages
Send messages to the Lite topic using the following publisher application:
Go
Before trying this sample, follow the Go setup instructions in the Pub/Sub Lite quickstart using client libraries. For more information, see the Pub/Sub Lite Go API reference documentation.
To authenticate to Pub/Sub Lite, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Java
Python
Before trying this sample, follow the Python setup instructions in the Pub/Sub Lite quickstart using client libraries. For more information, see the Pub/Sub Lite Python API reference documentation.
To authenticate to Pub/Sub Lite, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
The publisher sends 100 messages to a Lite topic and prints the number of messages that the Pub/Sub Lite service receives.
Receive messages
Receive messages from the Lite subscription using the following subscriber application:
Go
Before trying this sample, follow the Go setup instructions in the Pub/Sub Lite quickstart using client libraries. For more information, see the Pub/Sub Lite Go API reference documentation.
To authenticate to Pub/Sub Lite, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Java
Before trying this sample, follow the Java setup instructions in the Pub/Sub Lite quickstart using client libraries. For more information, see the Pub/Sub Lite Java API reference documentation.
To authenticate to Pub/Sub Lite, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Python
Before trying this sample, follow the Python setup instructions in the Pub/Sub Lite quickstart using client libraries. For more information, see the Pub/Sub Lite Python API reference documentation.
To authenticate to Pub/Sub Lite, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
After the subscriber receives a message, the subscriber prints the message ID and the message data.
Clean up
To avoid incurring charges to your Google Cloud account for the resources used on this page, delete the Google Cloud project with the resources.
- To avoid incurring charges to your Google Cloud account for the
resources used in this guide, delete the topic and subscription.
gcloud pubsub lite-subscriptions delete testSub --location=us-central1 gcloud pubsub lite-topics delete testTopic --location=us-central1
-
Optional: Revoke the authentication credentials that you created, and delete the local credential file.
gcloud auth application-default revoke
-
Optional: Revoke credentials from the gcloud CLI.
gcloud auth revoke
What's next
- Learn more about Lite topics and Lite subscriptions.
- Learn more about sending and receiving messages.
- Look through code samples for the client library.