Sal
Sal

Reputation: 1697

Do you need copies of protobufs in both client and server in web applications?

I'm not sure if this is the right forum to post this question, but I'm trying to learn gRPC/protobufs in the context of a web application. I am building the UI in Flutter, and the backend in Go with MongoDB. I was able to get a simple go service running and I was able to query it using Kreya, however my question now is - how do I integrate the UI with the backend? In order to make the Kreya call, I needed to import the protobufs. Do I need to maintain identical protobufs in both the front end and backend? Meaning, do I literally have to copy all of my protobufs in the backend into my UI codebase and compile locally there as well? This seems like a nightmare to maintain, as now the protobufs have to be maintained in two places, as opposed to one.

What is the best way to maintain the protobufs?

Upvotes: 1

Views: 2274

Answers (1)

DazWilkin
DazWilkin

Reputation: 40271

Yes, but think of the protos as a shared (contract) between your clients and servers.

The protos define the interface by which the client is able to communicate with the server. In order for this to be effective, the client and server need to implement the same interface.

One way to do this is to store your protos in a repo that you share in any clients and servers that implement it. This provides a single source of truth of the protos. I also generate copies of the protos compiled (protoc) to the languages I will use e.g. Golang, Dart etc. in this shared protos repo and import from the repo where needed.

Then, in your case, the client imports the Dart-generated sources and the Golang server imports the Golang-generated sources from the shared repo.

Alternatively, your client and your server could protoc compile appropriate sources when they need them, on-the-fly, usually as part of an automated build process.

Try not to duplicate the protos across clients and servers because it will make it challenging to maintain consistency; it will be challenging to ensure every copy remains synchronized.

Upvotes: 4

Related Questions