91

I'm trying to describe an RPC service using Google's Protocol Buffers

service WhoamiService {
  rpc WhoAreYou() returns (Whoami) {}
}

message Whoami {
  optional bytes request_id = 1;
  optional string hostname = 2;
  optional string message = 3;
}

When I try to compile this definition, I get an error Expected type name pointing to the WhoAreYou() piece.

It works fine if I replace WhoAreYou() with WhoAreYou(Whoami), but in this case, the method does not need any parameters.. Is there a way to do this or is it simply not supported?

Michael Robinson
  • 1,985
  • 2
  • 21
  • 31
  • visitors to this page might also be interested in https://stackoverflow.com/a/31772973/10278 and google.protobuf.Empty – pestophagous Sep 08 '17 at 00:55

2 Answers2

139

You have to specify an input type. If you don't want the method to take any parameters, define an empty message type, like:

message WhoAreYouParams {}

The reason this is required is so that if you later need to add an optional parameter, you can do so without breaking existing code.

Kenton Varda
  • 41,353
  • 8
  • 121
  • 105
  • I'm wondering, by "breaking existing code", you mean breaking at a source level, right? Compiled clients will simply ignore messages which suddenly have new fields in them. – Peter Štibraný Nov 15 '19 at 11:03
135

You can specify google.protobuf.Empty instead of your own empty message. Example:

rpc WhoAreYou(google.protobuf.Empty) returns (Whoami) {
}

Don't forget to import appropriate proto file:

import "google/protobuf/empty.proto";
victorkt
  • 13,992
  • 9
  • 52
  • 51
Alex
  • 2,361
  • 1
  • 20
  • 27
  • 3
    This is the more precise answer. – colm.anseo May 10 '19 at 22:51
  • 12
    It's not recommended to do because there is no way to change it later with backward compatibility. – Raz May 17 '19 at 20:36
  • 2
    "It's not recommended to do because there is no way to change it later with backward compatibility." –– I would assume that changing the request or response message from Empty to something non-empty wouldn't break existing users of this service. So we're only talking about breaking at source level, where suddenly you need to modify source code of clients using the service. Is that right? (I cannot find any documentation on evolving gRPC services) – Peter Štibraný Nov 15 '19 at 11:04
  • ** As well not supported in 2.6 ver – USer22999299 Oct 18 '20 at 06:27