# gRPC

{% embed url="<https://grpc.io/>" %}

## About

It is a framework for building robust and scalable communication between server applications.

Ideal for:

* Microservices. *(Communication between services)*
* Backend. *(*`server-to-server` *communication)*
* Mobile. *(*`mobile-to-backend` *communication)*

{% hint style="warning" %}
Browsers are not yet fully prepared to handle gRPC.
{% endhint %}

It has a couple of advantages over `REST`, like:

* Type safety.
  * `gRPC` enforces message structures defined in [protocol-buffers](https://kdongs.gitbook.io/kdocs/t-and-o-communication/sync-communication/grpc/protocol-buffers "mention"). This ensures compatibility between services and prevents unexpected data types from causing errors.
* Performance otimizations.
  * Uses a compact binary encoding format for messages, leading to faster transmissions and lower resource consumption compred to other `JSON`-based APIs.
* Cleaner developement experience.
  * It offers features like streaming `RPCs`, allowing for efficient handling of data transfers or real-time updates.

{% hint style="info" %}
`gRPC` can communicate over `HTTP/2`. This means it:

* Supports traffic in `binary`.
* Supports bi-directional communication. *(Between Client-Backend)*
  {% endhint %}

#### [Supported Languages](https://grpc.io/docs/languages/)

`gRPC` has official support in:

* `GO`
* `JAVA`
* `C`
  * Through C you can use:
    * `C++`
    * `Python`
    * `Ruby`
    * `PHP`
    * `C#`
    * `Node.js`
    * `Dart`

## RPC (Remote Procedure Call)

A `Client` calls the `Server`, with a request, invoking a procedure and the Server responds with a response.

## Communication Types

### Unary

Client makes a **single** `request` and the Server responds with a **single** `response`.

<img src="https://4041415357-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhJfAu7W4SbMhx6ZfsKYN%2Fuploads%2Fp7GL6IFTYNUdHtoOMoRs%2Ffile.excalidraw.svg?alt=media&#x26;token=264c7198-1854-4518-8198-fc6d0a7e2290" alt="" class="gitbook-drawing">

### Server Streaming

Client makes a **single** `request` and the Server responds **one or multiple** `responses`.

This helps the Client to use the data as it comes (piece by piece), and not have to wait the server to fully process and returns the hole data.

<img src="https://4041415357-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhJfAu7W4SbMhx6ZfsKYN%2Fuploads%2FWWHaYANLLVvhYL2hBTLe%2Ffile.excalidraw.svg?alt=media&#x26;token=f51da4d6-6532-4238-ab05-4bdb427569f3" alt="" class="gitbook-drawing">

### Client Streaming

Client makes **multiple** `requests` and the Server responds with a **single** `response`.

Like if you will upload a file, you may upload it in peaces. After the Server receives them all it process it and respond.

<img src="https://4041415357-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhJfAu7W4SbMhx6ZfsKYN%2Fuploads%2F7h9QbK5wwO8cZBe4aEAT%2Ffile.excalidraw.svg?alt=media&#x26;token=e548afda-c119-4d67-97b7-8a7af9c4ff61" alt="" class="gitbook-drawing">

### Bi Directional Streaming

Client makes **multiple** `requests` and the Server responds with **multiple** `responses`.

<img src="https://4041415357-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhJfAu7W4SbMhx6ZfsKYN%2Fuploads%2FVfrHu13iyWvLqPyz3dwW%2Ffile.excalidraw.svg?alt=media&#x26;token=60818ca2-b861-4ba2-82f8-1fab7573d9b0" alt="" class="gitbook-drawing">

## REST vs gRPC

<table data-card-size="large" data-view="cards"><thead><tr><th></th><th></th><th></th></tr></thead><tbody><tr><td><strong>REST</strong></td><td><ul><li>Text / Json</li><li>One-directional only</li><li>Higher latency</li><li><p>No contracts <em>(higher change of error)</em></p><ul><li><em>Like</em> <code>number</code> <em>as</em> <code>string</code><em>.</em></li></ul></li><li>No streaming support.</li></ul></td><td></td></tr><tr><td><strong>gRPC</strong></td><td><ul><li>Protocol Buffers</li><li>Bi-directional and Asyncronous</li><li>Lower lantency</li><li>Defined contract (<code>.proto</code>)</li><li>Streaming support.</li></ul></td><td></td></tr></tbody></table>
