# How we use gRPC to build a client/server system in Go

https://medium.com/pantomath/how-we-use-grpc-to-build-a-client-server-system-in-go-dd20045fa1c2

This post is a technical presentation on how we use gRPC (and Protobuf) to build a robust client/server system.

I won’t go into the details of why we chose gRPC as the main communication protocol between our client and server, a lot of great posts already cover the subject (like this one, this one or this one). But just to get the big picture: we are building a client-server system, using Go, that needs to be fast, reliable, and that scales (thus we chose gRPC). We wanted the communication between the client and the server to be as small as possible, as secure as possible and fully compliant with both ends (thus we chose Protobuf).

We also wanted to expose another interface on the server side, just in case we add a client that has no compatible gRPC implementation: a traditional REST interface. And we wanted that at (almost) no cost.

### Context

We will start building a very simple client/server system, in Go, that will just exchange dummy messages. Once both communicate and understand each other, we’ll add extra features, such as TLS support, authentication, and a REST API.

The rest of this articles assumes you understand basic Go programming. It also assumes that you have protobuf package installed, and the protoccommand available (once again, many posts cover that topic, and there’s the official documentation).
You will also need to install Go dependencies, such as the go implementation for protobuf, and grpc-gateway.

All the code shown in this post is available at https://gitlab.com/pantomath-io/demo-grpc. So feel free to get the repository, and use the tags to navigate in it. The repository should be placed in the src folder of your $GOPATH: $ go get -v -d gitlab.com/pantomath-io/demo-grpc$cd$GOPATH/src/gitlab.com/pantomath-io/demo-grpc

### Defining the protocol

git tag: init-protobuf-definition

First of all, you need to define the protocol, i.e. to define what can be said between client and server, and how. This is where Protobuf comes into play. It allows you to define two things: Services and Messages. A service is a collection of actions the server can perform at the client’s request, a messageis the content of this request. To simplify, you can say that service defines actions, while message defines objects.

Write the following in [api/api.proto](https://gitlab.com/pantomath-io/demo-grpc/blob/init-protobuf-definition/api/api.proto):

syntax = "proto3";package api;message PingMessage {  string greeting = 1;}service Ping {  rpc SayHello(PingMessage) returns (PingMessage) {}}