Sobes.tech
Junior — Middle

How does server-side streaming data transfer mode differ from a single call in gRPC?

sobes.tech AI

Answer from AI

In gRPC, several data transfer modes are distinguished:

  • Unary RPC: The client sends a single request to the server and receives a single response. This is the classic request-response.

  • Server Streaming RPC: The client sends one request, and the server responds with a stream of messages. The client reads messages as they arrive until the server finishes the stream.

Differences:

  • In unary calls, the exchange occurs once — request and response.
  • In server streaming, the server can send multiple messages in response to a single request, which is convenient for transmitting large volumes of data or real-time updates.

Example in Go (server streaming):

func (s *server) ListFeatures(req *pb.Rectangle, stream pb.RouteGuide_ListFeaturesServer) error {
    for _, feature := range features {
        if inRange(feature.Location, req) {
            if err := stream.Send(feature); err != nil {
                return err
            }
        }
    }
    return nil
}