Skip to content

Instantly share code, notes, and snippets.

@vulkoingim
Created May 16, 2019 17:43
Show Gist options
  • Save vulkoingim/fc6a5a5538d8b80ff2e16ef201846120 to your computer and use it in GitHub Desktop.
Save vulkoingim/fc6a5a5538d8b80ff2e16ef201846120 to your computer and use it in GitHub Desktop.
C++ example of PubSub grpc pull operations
#include <iostream>
#include <memory>
#include <grpc++/grpc++.h>
#include <grpc++/client_context.h>
#include <grpc++/create_channel.h>
#include <grpc++/security/credentials.h>
#include <grpc++/support/status.h>
#include <grpc/grpc.h>
#include <grpcpp/grpcpp.h>
#include <grpcpp/security/credentials.h>
#include "google/pubsub/v1/pubsub.grpc.pb.h"
#include "google/pubsub/v1/pubsub.pb.h"
auto main() -> int {
using grpc::ClientContext;
using google::pubsub::v1::Publisher;
using google::pubsub::v1::PublishRequest;
using google::pubsub::v1::PublishResponse;
using google::pubsub::v1::PubsubMessage;
using google::pubsub::v1::Subscriber;
using google::pubsub::v1::Subscription;
using google::pubsub::v1::ReceivedMessage;
auto json = "{}";
auto jwt = grpc::ServiceAccountJWTAccessCredentials(json, 3600);
auto creds = CompositeChannelCredentials(SslCredentials(grpc::SslCredentialsOptions()), jwt);
ClientContext ctx;
auto sub = std::make_unique<Subscriber::Stub>(grpc::CreateChannel("pubsub.googleapis.com", creds));
auto const sub_string = "projects/test/subscriptions/test-subscription";
PullRequest pullRequest;
pullRequest.set_subscription(sub_string);
pullRequest.set_max_messages(100);
PullResponse pullResponse;
const grpc::Status &pull = sub->Pull(&ctx, pullRequest, &pullResponse);
for(auto&m :pullResponse.received_messages()){
std::cout <<m.message().data()
<<+" ID: "+m.message().message_id()
<<+" ACK ID:"+m.ack_id()+"\n";
}
std::cout << pullResponse.received_messages_size();
if (!pull.ok()) {
std::cout << "Pulling message failed with error " + std::to_string(pull.error_code()) + ": " +
pull.error_message() << '\n';
}
}
#include <iostream>
#include <memory>
#include <grpc++/grpc++.h>
#include <grpc++/client_context.h>
#include <grpc++/create_channel.h>
#include <grpc++/security/credentials.h>
#include <grpc++/support/status.h>
#include <grpc/grpc.h>
#include <grpcpp/grpcpp.h>
#include <grpcpp/security/credentials.h>
#include "google/pubsub/v1/pubsub.grpc.pb.h"
#include "google/pubsub/v1/pubsub.pb.h"
auto main() -> int {
using grpc::ClientContext;
using google::pubsub::v1::Publisher;
using google::pubsub::v1::PubsubMessage;
using google::pubsub::v1::Subscriber;
using google::pubsub::v1::Subscription;
using google::pubsub::v1::ReceivedMessage;
using google::pubsub::v1::StreamingPullRequest;
using google::pubsub::v1::StreamingPullResponse;
using grpc::Status;
auto json = "{}";
auto jwt = grpc::ServiceAccountJWTAccessCredentials(json, 3600);
auto creds = CompositeChannelCredentials(SslCredentials(grpc::SslCredentialsOptions()), jwt);
ClientContext ctx;
auto sub = std::make_unique<Subscriber::Stub>(grpc::CreateChannel("pubsub.googleapis.com", creds));
auto const sub_string = "projects/test/subscriptions/test-subscription";
StreamingPullRequest pullRequest;
pullRequest.set_subscription(sub_string);
auto pull = sub->StreamingPull(&ctx);
pull->Write(pullRequest);
pull->WritesDone();
StreamingPullResponse pullResponse;
while (pull->Read(&pullResponse)) {
std::cout << "PULLING\n\n";
std::cout << pullResponse.received_messages_size();
for (auto &m : pullResponse.received_messages()) {
std::cout << m.message().data()
<< +" ID: " + m.message().message_id()
<< +" ACK ID:" + m.ack_id() + "\n";
}
}
Status status = pull->Finish();
if (!status.ok()) {
std::cout << "Pulling message failed with error " + std::to_string(status.error_code()) + ": " +
status.error_message() << '\n';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment