Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(pubsub): add a subscriber quickstart #12053

Merged
merged 10 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions google/cloud/pubsub/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ int main(int argc, char* argv[]) try {

// Create a namespace alias to make the code easier to read.
namespace pubsub = ::google::cloud::pubsub;

auto publisher = pubsub::Publisher(
pubsub::MakePublisherConnection(pubsub::Topic(project_id, topic_id)));
auto id =
Expand Down
10 changes: 10 additions & 0 deletions google/cloud/pubsub/quickstart/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,13 @@ cc_binary(
"@google_cloud_cpp//:pubsub",
],
)

cc_binary(
name = "subscriber_quickstart",
srcs = [
"subscriber_quickstart.cc",
],
deps = [
"@google_cloud_cpp//:pubsub",
],
)
5 changes: 4 additions & 1 deletion google/cloud/pubsub/quickstart/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ else ()
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL")
endif ()

# Once the pubsub_client package is found, define new targets.
# Once the pubsub_client package is found, define new targets and the
# dependencies they link.
add_executable(quickstart quickstart.cc)
target_link_libraries(quickstart google-cloud-cpp::pubsub)
add_executable(subscriber_quickstart subscriber_quickstart.cc)
target_link_libraries(subscriber_quickstart google-cloud-cpp::pubsub)
7 changes: 5 additions & 2 deletions google/cloud/pubsub/quickstart/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ CXXFLAGS=
CXXLD=$(CXX)
BIN=.

all: $(BIN)/quickstart
all: $(BIN)/quickstart $(BIN)/subscriber_quickstart

# Configuration variables to compile and link against the Cloud Pub/Sub C++
# client library.
Expand All @@ -31,6 +31,9 @@ PUBSUB_CXXFLAGS := $(shell pkg-config $(PUBSUB_DEPS) --cflags)
PUBSUB_CXXLDFLAGS := $(shell pkg-config $(PUBSUB_DEPS) --libs-only-L)
PUBSUB_LIBS := $(shell pkg-config $(PUBSUB_DEPS) --libs-only-l)

# A target using the Cloud Pub/Sub C++ client library.
# Define 2 targets, one for the publisher and one for the subscriber, using the Cloud Pub/Sub C++ client library.
$(BIN)/quickstart: quickstart.cc
$(CXXLD) $(CXXFLAGS) $(PUBSUB_CXXFLAGS) $(PUBSUB_CXXLDFLAGS) -o $@ $^ $(PUBSUB_LIBS)

$(BIN)/subscriber_quickstart: subscriber_quickstart.cc
$(CXXLD) $(CXXFLAGS) $(PUBSUB_CXXFLAGS) $(PUBSUB_CXXLDFLAGS) -o $@ $^ $(PUBSUB_LIBS)
23 changes: 17 additions & 6 deletions google/cloud/pubsub/quickstart/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ Setting this environment variable is the recommended way to configure the
authentication preferences, though if the environment variable is not set, the
library searches for a credentials file in the same location as the [Cloud
SDK](https://cloud.google.com/sdk/). For more information about *Application
Default Credentials*, see
https://cloud.google.com/docs/authentication/production
Default Credentials*, see https://cloud.google.com/docs/authentication/production

## Using with Bazel

Expand All @@ -71,12 +70,18 @@ https://cloud.google.com/docs/authentication/production
Note that, as it is often the case with C++ libraries, compiling these
dependencies may take several minutes.

1. Run the example, changing the placeholder(s) to appropriate values:
1. Publish messages by running the example, changing the placeholder(s) to appropriate values:

```bash
bazel run :quickstart -- [GCP PROJECT ID] [PUB/SUB TOPIC ID]
```

1. Subscribe to messages by running the example, changing the placeholder(s) to appropriate values:

```bash
bazel run :subscriber_quickstart -- [GCP PROJECT ID] [PUB/SUB SUBSCRIPTION ID]
```

## Using with CMake

> :warning: If you are using Windows or macOS there are additional instructions
Expand Down Expand Up @@ -108,10 +113,16 @@ https://cloud.google.com/docs/authentication/production
cmake --build .build
```

1. Run the example, changing the placeholder(s) to appropriate values:
1. Publish messages by running the example, changing the placeholder(s) to appropriate values:

```bash
.build/quickstart [GCP PROJECT ID] [PUB/SUB TOPIC ID]
```

1. Subscribe to messages by running the example, changing the placeholder(s) to appropriate values:

```bash
.build/quickstart [GCP PROJECT ID] [PUB/SUB TOPIC ID]
.build/subscriber_quickstart [GCP PROJECT ID] [PUB/SUB SUBSCRIPTION ID]
```

## Platform Specific Notes
Expand Down Expand Up @@ -152,4 +163,4 @@ set GRPC_DEFAULT_SSL_ROOTS_FILE_PATH=%cd%\roots.pem
[grpc-roots-pem-bug]: https://github.com/grpc/grpc/issues/16571
[homebrew-cmake-link]: https://formulae.brew.sh/formula/cmake
[howto-setup-dev-workstation]: /doc/contributor/howto-guide-setup-development-workstation.md
[pubsub-quickstart-link]: https://cloud.google.com/pubsub/docs/quickstart-console
[pubsub-quickstart-link]: https://cloud.google.com/pubsub/docs/publish-receive-messages-client-library
alevenberg marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions google/cloud/pubsub/quickstart/quickstart.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ int main(int argc, char* argv[]) try {

// Create a namespace alias to make the code easier to read.
namespace pubsub = ::google::cloud::pubsub;

auto publisher = pubsub::Publisher(
pubsub::MakePublisherConnection(pubsub::Topic(project_id, topic_id)));
auto id =
Expand Down
57 changes: 57 additions & 0 deletions google/cloud/pubsub/quickstart/subscriber_quickstart.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! [START pubsub_quickstart_subscriber] [all]
#include "google/cloud/pubsub/message.h"
#include "google/cloud/pubsub/subscriber.h"
#include <iostream>

int main(int argc, char* argv[]) try {
if (argc != 3) {
std::cerr << "Usage: " << argv[0] << " <project-id> <subscription-id>\n";
return 1;
}

std::string const project_id = argv[1];
std::string const subscription_id = argv[2];

auto constexpr kWaitTimeout = std::chrono::seconds(30);

// Create a namespace alias to make the code easier to read.
namespace pubsub = ::google::cloud::pubsub;

auto subscriber = pubsub::Subscriber(pubsub::MakeSubscriberConnection(
pubsub::Subscription(project_id, subscription_id)));

auto session =
subscriber.Subscribe([&](pubsub::Message const& m, pubsub::AckHandler h) {
std::cout << "Received message " << m << "\n";
std::move(h).ack();
});

std::cout << "Waiting for messages on " + subscription_id + "...\n";

// Blocks until the timeout is reached.
auto result = session.wait_for(kWaitTimeout);
if (result == std::future_status::timeout) {
std::cout << "timeout reached, ending session\n";
session.cancel();
}

return 0;
} catch (google::cloud::Status const& status) {
std::cerr << "google::cloud::Status thrown: " << status << "\n";
return 1;
}
//! [END pubsub_quickstart_subscriber] [all]