Proto buffs
Course: GRPC - chapter 4
Protocol buffers are Google’s language-neutral, platform-neutral, extensible mechanism for serializing structured data – think XML, JSON.
The protocol buffers are where we define our service definitions and messages. This will be like a contract or common interface between the client and server on what to expect from each other; the methods, types, and returns of what each operation would bear.
- Schema Definition Language: Protocol Buffers use a schema definition language (
**.proto**files) to define the structure of data. This language allows you to specify message types, fields, enums, and services. - Binary Serialization: Protocol Buffers serialize data into a binary format, which is more compact and efficient compared to text-based formats like XML and JSON.
- Language Support and Code Generation: Protocol Buffers support code generation for a wide range of programming languages, including C++, Java, Go, Python, JavaScript, Ruby, and more. Protocol Buffers come with tools (e.g.,
**protoc**) that generate code in various programming languages based on your**.proto**files.
Let’s create a simple Proto file
message.proto
syntax = "proto3";
// Define a message type representing a person.message Person { string name = 1; int32 age = 2;}
service PersonService { // Add a person to the address book. rpc AddPerson(Person) returns (Person);
// Get a person from their name rpc GetPersonByName(GetPersonByNameRequest) returns (Person);}
message GetPersonByNameRequest { string name = 1;}There are a few things to unpack here -
-
message- A message that can be encoded/decoded/transferred -
types
- string
- int32
-
service - Describes what all
rpcmethods you support -
Field numbers In Protocol Buffers, each field within a message type is assigned a unique numerical identifier called a tag or field number. These field numbers serve several purposes:
- Efficient Encoding: Field numbers are used during serialization and deserialization to efficiently encode and decode the data. Instead of including field names in the serialized data, Protocol Buffers use field numbers, which are typically more compact and faster to process.
- Backward Compatibility: Field numbers are stable identifiers that remain consistent even if you add, remove, or reorder fields within a message type. This means that old serialized data can still be decoded correctly by newer versions of your software, even if the message type has changed.
- Language Independence: Field numbers provide a language-independent way to refer to fields within a message type. Regardless of the programming language used to generate the code, the field numbers remain the same, ensuring interoperability between different implementations.
Seializing and deserializing data (easy)
Section titled “Seializing and deserializing data (easy)”
Serializing and deserializing data (hard)
Section titled “Serializing and deserializing data (hard)”- Create a.proto
- npm init -y
- npm i protobufjs
const protobuf = require('protobufjs');
// Load the Protocol Buffers schemaprotobuf.load('a.proto') .then(root => { // Obtain the Person message type const Person = root.lookupType('Person');
// Create a new Person instance const person = { name: "Alice", age: 30 };
// Serialize Person to a buffer const buffer = Person.encode(person).finish();
// Write buffer to a file require('fs').writeFileSync('person.bin', buffer);
console.log('Person serialized and saved to person.bin');
// Read the buffer from file const data = require('fs').readFileSync('person.bin');
// Deserialize buffer back to a Person object const deserializedPerson = Person.decode(data);
console.log('Person deserialized from person.bin:', deserializedPerson); }) .catch(console.error);-
Check the size of person.bin

-
Create a
person.jsonfile and check it’s size
{ name: "Alice", age: 31}