RPC: Remote Procedure Calls

Open sourced on github.

In gRPC, a client application can directly call a method on a server application on a different machine as if it were a local object, making it easier for you to create distributed applications and services.

How gRPC works

  • As in many RPC systems, gRPC is based around the idea of defining a service, specifying the methods that can be called remotely with their parameters and return types.
  • On the server side, the server implements this interface and runs a gRPC server to handle client calls.
  • On the client side, the client has a stub (referred to as just a client in some languages) that provides the same methods as the server.
  • gRPC clients and servers can run and talk to each other in a variety of environments - from servers inside Google to your own desktop - and can be written in any of gRPC’s supported languages.

Four types of class from gRPC

Unity calls

Send request as a client, get a single response.

Server and client streaming

  • Send as many request as I want, and get one final response from the server;
  • Send one initial request and get multiple responses from the server

    Full bidirectional streaming

    both directions are streaming (multiple messages)

Protocol Buffers

Protobuf is the IDL, Interface Definition Language, used by gRPC.

Server Side

This is the greet.proto file built by .NET‘s gRPC service template:

service Greeter defines what service is available. rpc defines the “process” and the request/reply that the process takes in and returns.

Proto will generate C# code for services.

Server side StartUp

  • configureServices makes a call to AddGrpc
  • configure builds the app’s routing endpoints.
    • in here you should register all of your services.

Client Side

For the client side we need a using var channel = GrpcChannel.ForAddress("https://localhost:5001");
Then we create a client with var client = new GreeterClient(channel);
Now we can involve the task that is available from the server, and pass it the request: var request = new HellowRequest {Name = "anran" }; var response = await client.SayHellowAsync(request);
To print the message we can now do: Console.WriteLine(response.Message);

gRPC is an opinionated protocol

Many things are built in into the protocol. e.g. You don’t need to write down the start path of the Service, as long as the Client knows the existence of the endpoint.