protocol-buffersgrpcproto3

Defining a single RPC with both a streaming and unary response


Is it possible to define a single proto3 RPC that can have either a streaming response or a unary response? Like when I define the RPC I can make it clear that the stream keyword is optional?

If streaming is requested, then the response content will be streamed back to the client. If streaming is not requested, then a single response with all content will be returned.

There's other options, like defining two separate RPCs for stream vs. no stream or just return a single response within the stream (the client would still need to consider it a streaming API though AFAICT, not sure about the implications there).

What's the right way to get this behaviour?


Solution

  • The gRPC-specific syntax permits stream to only be applied to rpc definitions and there's no optional stream.

    You could return a stream containing a single message. This scenario would be better suited to the client being unaware of how many messages the server may return.

    You could define two rpcs (unary and server streaming). This scenario would be better suited to the client knowing that it will receive a single message response.