Starting The Microservice Era on iOS Using Protobuf and gRPC

Satrio Wicaksono
Life At Moka
Published in
5 min readDec 5, 2019

Technology is always getting a step ahead to fulfill the-always-changing and complicated business’ needs around the world. If we’re talking about technology, don’t forget to invoke the architecture that builds how technology was established. Microservice model is a good foundation that made technology established with fewer dependencies, and more scalable.

Certainly, Protobuf and gRPC were born to fit the microservice model. By the way, do you know that it was created and introduced by Google? Everyone knows that everything born from Google back then was everyone’s favorites and recommendations.

Protocol Buffer or in short Protobuf is a way of encoding structured data in an efficient yet extensible format. Google developed Protobuf to be used in their internal services. It is a binary encoding format that allows you to specify a schema for your data using specification language.

An instance snippet of Protobuf Language

The snippet defined a schema for a person’s data type that has three fields: ID, name, and email. In addition to naming a field, we can provide a type that will determine how the data is encoded and sent over the wire. In the image above, we see an int32 type and a string type. Keywords for validation and structure are also provided (required and optional).

The Protobuf is able to be implemented in various languages, such as; Java, C, Go, Swift, etc. And most modern languages can also be implemented if we have the will to do some research. These are the 4 reasons in considering Protobuf in an microservice environment.

1. Schemas are cool

In JSON format, we have to be careful and continue to maintain our data models if there is modification data response from the service. It’s absolutely painful to avoid our app from getting into trouble. By encoding the semantics of your business objects in proto (Protobuf format file) format, it’s enough to help in ensuring that the signal doesn’t get lost between applications and the boundaries we create enforces our business rules.

2. Say goodbye to boilerplate code

Parser models and Presenter classes often contain hidden business logic and expose the fragile nature of hand parsing each new data type when a stub class as generated by Protobuf (that we generally never have to touch) can provide much of the same functionality without all of the headaches. Once we generate .proto, we can be more focused on keeping our application going and improve our product.

3. Validation and Extensibility

The required, optional, and repeated keywords in Protobuf definitions are extremely powerful. They allow us to encode, at the schema level, the shape of our data

structure and the implementation details of how classes work in each language

4. Fully support for using gRPC

gRPC is only able to be set up in Protobuf. The Protobuf will be more powerful and more useful when combined with gRPC. The data can be serialized stream through Protobuf combining with gRPC.

We talked a bit about gRPC on four reasons above in considering Protobuf. Now, let me tell you about what is gRPC. ✌️

gRPC is a modern open-source high-performance RPC framework that can run in any environment. It can efficiently connect services in and across data centers. It is also applicable in the last mile of distributed computing to connect devices, mobile applications, and browsers to backend services.

gRPC is supported by many modern languages such as: Go, Kotlin, Swift, NodeJs, etc. Hmm… what? Swift and Kotlin? Yeah, exactly! gRPC is available to run on mobile iOS and Android. And now we will expose how do we use gRPC in our mobile app, especially for iOS app.

Firstly, if we want to know about the advantages of using gRPC than REST, you may touch down first to grpc.io.

Figure gRPC Schema

The example of gRPC schema can be seen above. The schema shows how gRPC that is written by Java and has 2 clients, which are NodeJs and Swift iOS. gRPC has 4 kinds of RPC Services: Unary Call Service, Client Stream Call Service, Server Stream Call Service, and Bidirectional Stream Call Service.

The Unary Call Service is like the REST mechanism. Where the client side sends a request to the server, and the server-side sends a response to the client. Client Stream Call Service is where the client-side sends a request as a stream to the server, and the server-side sends a response to the client. Server Stream Call Service is where the client-side sends a request to the server, and the server-side sends a response as a stream to the client. And Bidirectional Stream Call Service (Client Server Stream) is where the client-side sends a request as stream to the server, and the server-side sends a response as a stream to the client.

We provide 2 repositories to show how gRPC works on iOS Swift. One repository is as a server and written by Java code. You can start to clone the repository here. And the other repository is as an iOS App which was written by Swift code. You can start to clone the repository here.

Snapshot of gRPC Demo in Swift and Java

From the above’s video, I hope everyone can have a more broadened overview of the repositories. After you cloned those, hopefully, you can try to run the projects and learn how gRPC works on iOS Swift.

Protobuf and gRPC will play an interesting part in our journey. With them, we can more easily handle changes response from backend service. And by using stream mechanism from gRPC, we can do matters about sync data between web and iOS App! 🙌

--

--