gRPC — Message Types

Most important thing is to have in mind is all of the rules in Protocol Buffers applies here. Whatever you can do in protocol buffers you could do it with gRPC.

If you have no experience with Protocol buffers, have a look at this

service Student {
rpc GetByStudentNumber (Request) returns (Response)

Above is the way to define a simple service in gRPC. I will explain one by one.

  1. service — Keyword to define a gRPC service
  2. Student — Service name
  3. rpc — defines RPC calls ( what the client ask the server )
  4. GetByStudentNumber — Method name
  5. Request — Has to be a message type (no void or primitive data types)
  6. Response — Has to be a message type (no void or primitive data types)

This is what happens once you finish writing the services. The file contains the service for example in the above example I started the service Student. So will call that file as “StudentService”. This service will be inside a protocol buffer message definition.

Yeah !! That’s the name we call it. The Protocol Buffer Message Definition.

When we call the Protocol Buffer compiler it gonna generate the “Client Stub”

Client Stub - Generated source code to the client in order to communicate with the server

and the “Server Interface”.

Server Interface - This has two parts, service we can implement, and another part to handle the serialization and de-serialization during the communication.

Don’t worry gRPC will handle all the serialization part for the communication.

Let’s see an example.

Above example has all four types of messages in gRPC. Unary, Client streaming, Server Streaming, Bidirectional streaming.

First we must install protocol buffer compiler to work with.

here is my article about installing. Check this out.

Once we have the protocol buffer compiler ready to work we can start defining the service definition.

Cheers !!!



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store