c++asynchronousstreaminggrpc

How to receive messages from a streaming server?


I am trying to build an asynchronous gRPC C++ client that sends/receives streaming messages to/from server using the ClientAsyncReaderWriter instance. The client and the server send messages to each other whenever they want. How can I check if there is any message from the server?

The ClientAsyncReaderWriter instance has a binded completion queue. I tried to check the completion queue by calling Next() and AsyncNext() functions to see if there is any event that would indicate that there is some message from the server. However, the completion queue has no events even if there is a message from the server.

class AsyncClient {
public:
    AsyncClient(std::shared_ptr<grpc::Channel> channel) :
        stub_(MyService::NewStub(channel)),
        stream(stub_->AsyncStreamingRPC(&context, &cq, (void *)1))
    {}
    ~AsyncClient()
    {}

    void receiveFromServer() {
        StreamingResponse response;

        // 1. Check if there is any message
        // 2. Read the message
    }

private:
    grpc::ClientContext context;
    grpc::CompletionQueue cq;
    std::shared_ptr<MyService::Stub> stub_;
    std::shared_ptr<grpc::ClientAsyncReaderWriter<StreamingRequest, StreamingResponse>> stream;
};

I need to implement steps 1 and 2 in the receiveFromServer() function.


Solution

  • Fortunately, I found a solution to my problem and the asynchronous bi-directional streaming in my project works as expected now. It turned out that my understanding of the "completion queue" concept was incorrect. This example was a great help for me!