Publish messages from C++ using gRPC

1,339 views
Skip to first unread message

Manuel Bergler

unread,
Sep 7, 2017, 10:27:49 AM9/7/17
to Google Cloud Pub/Sub Discussions
Hi everyone,

I'm trying to publish messages to Google PubSub topic from a C++ application. So far I've been able to get it to work with the emulator, but when trying to send messages to Google's API, the gRPC request always fails with error 14: Endpoint read failed.

I suspect that I'm not authenticating correctly. So far I've created a service account that is allowed to publish, and I've set GOOGLE_APPLICATION_CREDENTIALS to point to the .json file with the credentials.

My code looks like this:

#include <iostream>
#include <memory>

#include <grpc++/grpc++.h>

#include "google/pubsub/v1/pubsub.grpc.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;

   
auto creds = grpc::GoogleDefaultCredentials();
   
auto stub = std::make_unique<Publisher::Stub>(grpc::CreateChannel("pubsub.googleapis.com", creds));

   
PublishRequest request;
   
auto const topic_string = "projects/pubsub-cpp-api-1504713535863/topics/testing";
    request
.set_topic(topic_string);
   
PubsubMessage msg;
    msg
.set_data("Hello from C++");
   
*request.add_messages() = msg;

   
PublishResponse response;
   
ClientContext ctx;

   
auto status = stub->Publish(&ctx, request, &response);
   
if (!status.ok()) {
        std
::cout << "Publishing message failed with error " + std::to_string(status.error_code()) + ": " + status.error_message() << '\n';
   
}
}

What am I missing?

Best regards
Manuel 

Alex Mordkovich

unread,
Sep 7, 2017, 10:40:25 AM9/7/17
to Manuel Bergler, Google Cloud Pub/Sub Discussions
Hi Manuel,
Check your client for connectivity issues, and make sure that it's talking to the https endpoint: https://pubsub.googleapis.com/

Cheers,
Alex

--
You received this message because you are subscribed to the Google Groups "Google Cloud Pub/Sub Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cloud-pubsub-discuss+unsub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cloud-pubsub-discuss/dac8c3ad-4bd1-44ab-9a8c-185001150256%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Manuel Bergler

unread,
Sep 7, 2017, 11:02:16 AM9/7/17
to Google Cloud Pub/Sub Discussions
Am Donnerstag, 7. September 2017 16:40:25 UTC+2 schrieb Alex Mordkovich:
Hi Manuel,
Check your client for connectivity issues, and make sure that it's talking to the https endpoint: https://pubsub.googleapis.com/

Using "https://pubsub.googleapis.com" in the call to `grpc::CreateChannel` doesn't work, as it causes DNS resolution to fail with error message "Servname not supported for ai_socktype".
I've used wireshark now to verify that gRPC uses https, which it does. The same wireshark capture also shows that pubsub.googleapis.com responded, but also revealed an TLS alert (Level: Fatal, Description: Protocol Version). Do I need to add a specific cipher to GRPC_SSL_CIPHER_SUITES?

Manuel Bergler

unread,
Sep 7, 2017, 1:53:12 PM9/7/17
to Google Cloud Pub/Sub Discussions
Ok, I'm stupid. I linked against gRPC::grpc++_unsecure as I simply copied and then modified the CMakeLists.txt from one of the examples. Linking against gRPC::grpc++ fixed the problem.
Reply all
Reply to author
Forward
0 new messages