The Pub/Sub service allows applications to exchange messages reliably, quickly, and asynchronously. To accomplish this, a producer of data publishes a message to a Pub/Sub topic. A subscriber client then creates a subscription to that topic and consumes messages from the subscription. Pub/Sub persists messages that could not be delivered reliably for up to seven days. This page shows you how to get started publishing messages with Pub/Sub using client libraries.
Before you begin
-
Sign in to your Google Account.
If you don't already have one, sign up for a new account.
-
Set up a Cloud Console project.
Click to:
- Create or select a project.
- Enable the Pub/Sub API for that project.
- Create a service account.
- Download a private key as JSON.
You can view and manage these resources at any time in the Cloud Console.
-
Set the environment variable
GOOGLE_APPLICATION_CREDENTIALS
to the path of the JSON file that contains your service account key. This variable only applies to your current shell session, so if you open a new session, set the variable again. - Install and initialize the Cloud SDK.
Installing the client libraries
The following samples show you how to install the client libraries:
Python
For more on setting up your Python development environment, refer to Python Development Environment Setup Guide.
# ensure that you are using virtualenv
# as described in the python dev setup guide
pip install --upgrade google-cloud-pubsub
C++
For more information about installing the C++ library,
see the GitHub README
.
C#
Install-Package Google.Cloud.PubSub.V1 -Pre
Go
go get -u cloud.google.com/go/pubsub
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 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.
Node.js
npm install --save @google-cloud/pubsub
PHP
composer require google/cloud-pubsub
Ruby
gem install google-cloud-pubsub
Creating a topic and a subscription
Once you create a topic, you can subscribe or publish to it.
Use the gcloud pubsub topics create command to create a topic:
gcloud pubsub topics create my-topic
Use the gcloud pubsub subscriptions create command to create a subscription. Only messages published to the topic after the subscription is created are available to subscriber applications.
gcloud pubsub subscriptions create my-sub --topic my-topic
For more information about naming your topics and subscriptions, see Resource names.
Publishing messages
In the following samples, use my-topic
for your topic ID.
Python
C++
C#
Go
Java
Node.js
PHP
Ruby
Receiving messages
Set up a subscriber to pull the messages you just published. Every subscriber must acknowledge each message within a configurable time window. Unacknowledged messages are redelivered. Note that Pub/Sub occasionally delivers a message more than once to ensure that all messages make it to a subscriber at least once.
In the following samples, use my-sub
for your subscription ID.
Python
C++
C#
Go
Java
Node.js
PHP
Ruby
How did it go?
Clean up (optional)
To avoid incurring charges to your Google Cloud Platform account for the resources used in this guide, you can use the command line to delete the topic and subscription.
gcloud pubsub subscriptions delete my-sub gcloud pubsub topics delete my-topic
What's next
See What is Pub/Sub?.
For an end-to-end example, see Building a Pub/Sub system.
For additional use cases, see the Pub/Sub tutorials.
To get details about the client library for your language of choice, see Client Libraries.
To learn more about the concepts discussed in this page, see the Publisher and Subscriber guides.