Skip to content

Instantly share code, notes, and snippets.

@yang-g
Last active March 21, 2020 00:31
Show Gist options
  • Save yang-g/257d758cdb990eec42f8 to your computer and use it in GitHub Desktop.
Save yang-g/257d758cdb990eec42f8 to your computer and use it in GitHub Desktop.
grpc pubsub c++ example
#include <iostream>
#include <grpc++/grpc++.h>
#include <grpc++/security/credentials.h>
#include "pubsub.grpc.pb.h"
int main(int argc, char** argv) {
auto channel = grpc::CreateChannel(“pubsub.googleapis.com”, grpc::GoogleDefaultCredentials());
auto stub = google::pubsub::v1::Publisher::NewStub(channel);
google::pubsub::v1::ListTopicsRequest request;
google::pubsub::v1::ListTopicsResponse response;
grpc::ClientContext context;
request.set_page_size(10);
request.set_project("project_name");
auto status = stub->ListTopics(&context, request, &response);
if (status.ok()) {
for (const auto& topic : response.topics()) {
std::cout << topic.name() << std::endl;
}
} else {
std::cout << “rpc failed.” << std::endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment