Protocol Buffers in Two Minutes

Gamze Yılan
Nerd For Tech
Published in
3 min readJul 14, 2022

--

This article will teach you all that you need to know in order to get started with Protocol Buffers, just in two minutes.

What are Protocol Buffers?

Protocol Buffers (ProtoBuf for short) is a method of serializing data (turning it into binary) in order to be able to transmit it. Thanks to ProtoBuf we can translate data to different languages with something as simple as runnning a command. Hence, it’s very convenient for microservices.

While a, let’s say, Json data is not structured and does not need a schema the ProtoBuf does. Meaning you don’t need to define data type and stick to it for each attribute of an object with Json, but you do with ProtoBuf.

Getting Started

To start using ProtoBuf, you must first create a file with the .proto extension. Then, the first line in that said file should be:

syntax = “proto3”; //You may write whatever proto versiın you’re using here.

Each object is a message with the ProtoBuf, and we must define both the objects and their exact types in our .proto file. For each property within a message we must also assign an id so that the ProtoBuf can decode and encode it. So if we’re creating a Student object it should be as:

message Student {

int32 id = 1;

string name = 2;

float gpa = 3;}

And if we’re creating an array of Student class’ objects it should be as:

message Students {

repeated Student students = 1; }

We must also create a service for each controller within our app, and for each function in our controller we must put an rpc in our service as:

service ControllerName{

rpc FunctionName(whatModelItTakes) retuns (whatModelitReturns); }

Now in order to be able to change this to any language we’d like, we need to first get the protoc — the Proto Buffer compiler. You can install it here on this link. Simply select the one that suits your operating system and don’t worry about the ones with the languages for now. Move it under the C:/ and extract it, and then open the terminal, navigate to the project folder and run the command:

C:\protoc-21.2-win64\bin\protoc — js_out=import_style=commonjs,binary:. protoFileName.proto

Note: You can change the commonjs part to whatever language you’d like, but that one is for translating it to JavaScript.

The command above will create a protoFileName_pb.languageExtension (ie. protoFileName.js) file within your project directory. At this point we can start actually using these classes in whatever language we’ve created the protobuf for. But first, make sure to install google-protobuf library to whatever language your project is in. Then import the protoFileName_pb file on each file that’s going to use it.

You can now simply initialize and create objects from that schema. For any object created from protobuf, you will now have pre-defined methods to translate it to any language such as:

students.serializeBinary(); // to translate it into binary

students.deserializeBinary(); //to convert it back to the original form

--

--