How to return an array in Protobuf service rpc
45,345
Option 1 - Use stream:
rpc GetItems (ItemsQuery) returns (stream Item) {
}
Option 2 - Set a response message which will use a repeated object:
service MyService {
rpc GetItem (ItemQuery) returns (ItemResponse) {
}
}
message ItemQuery {
int id = 1;
}
message ItemResponse {
repeated Item items = 1;
}
message Item {
int id = 1;
string name = 2;
}
Author by
Shoham
Updated on May 18, 2020Comments
-
Shoham about 4 years
I have the following schema in my
.proto
file:service MyService { rpc GetItem (ItemQuery) returns (Item) { } } message ItemQuery { int id = 1; } message Item { int id = 1; string name = 2; }
Now I want to add another rpc method to return multiple Items. Something like this:
rpc GetItems (ItemsQuery) returns (repeated Item) { }
Is there a better way to do it than define an Items message?
-
LuMa almost 7 yearsI'm late for the party, but: Does option 2 have any advantages over option 1 or vice versa?
-
Shoham over 6 yearsOption 1 is a stream, it means you are returning an iterator and that means you can start processing the Items on client even before the server has finished sending all of them. Option 2 is a response object which contains a list of your Items. You can add some other properties to your ItemResponse (some metadata etc)...
-
Jaward Sally about 3 yearsHi, Is there anyway we can return a key less array response as the response in gRPC? I tried stream, but it doesn't help.
-
Vladimir almost 3 yearsWorks wine with:
message GetChannelsResponse {repeated string channel_name = 1;}