Slide 1

Slide 1 text

gRPC with Node.js maybe it's time to put ReST to rest

Slide 2

Slide 2 text

blog.lsantos.dev cloud advocate_ who am I_ [email protected]

Slide 3

Slide 3 text

What is gRPC?

Slide 4

Slide 4 text

gRPC - Developed by Google in 2015 - Uses HTTP/2 for transport - RPC stands for "remote procedure call" - Directly calls a method on the server as if it were a local object - Uses protobuf as IDL - Interoperable among languages

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

RPC is NOT new

Slide 7

Slide 7 text

JRMI - Java Remote Method Invocation Client Remote object Interface Stub Remote Object Implementation Remote object Interface Skeleton Client Server Implemented by the developer Implemented by JRMI

Slide 8

Slide 8 text

Protocol Buffers protobuf for the bffs <3

Slide 9

Slide 9 text

protobuf - OSS, Developed by Google - Mechanism for serializing structured data - Interface Definition Language - Requires compiling

Slide 10

Slide 10 text

message.proto

Slide 11

Slide 11 text

Compile time!

Slide 12

Slide 12 text

Outfile

Slide 13

Slide 13 text

message_pb.js

Slide 14

Slide 14 text

Generated Methods

Slide 15

Slide 15 text

Then we can just use

Slide 16

Slide 16 text

gRPC != Protobuf

Slide 17

Slide 17 text

Protobuf is universal_

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

gRPC is based on services_

Slide 20

Slide 20 text

Defining a service

Slide 21

Slide 21 text

Defining a service

Slide 22

Slide 22 text

Types of methods_

Slide 23

Slide 23 text

Unary

Slide 24

Slide 24 text

Server Streaming

Slide 25

Slide 25 text

Client Streaming

Slide 26

Slide 26 text

Bidirectional

Slide 27

Slide 27 text

Building the server with NodeJS

Slide 28

Slide 28 text

Install grpc

Slide 29

Slide 29 text

One small change

Slide 30

Slide 30 text

Load the protofile

Slide 31

Slide 31 text

Load the protofile "Database"

Slide 32

Slide 32 text

Implement the functions on the serverside

Slide 33

Slide 33 text

Implement the functions on the serverside

Slide 34

Slide 34 text

Start the server

Slide 35

Slide 35 text

Full code

Slide 36

Slide 36 text

Full code load

Slide 37

Slide 37 text

Full code load database

Slide 38

Slide 38 text

Full code load database implementation

Slide 39

Slide 39 text

Full code load database implementation run

Slide 40

Slide 40 text

Creating the client

Slide 41

Slide 41 text

Install grpc

Slide 42

Slide 42 text

Load the protofile

Slide 43

Slide 43 text

Add the service

Slide 44

Slide 44 text

Call it!

Slide 45

Slide 45 text

Full Code

Slide 46

Slide 46 text

DEMO

Slide 47

Slide 47 text

Problems - Docs are not very common - The initial development is more complex - Usage with TypeScript requires some work - It is a bit complicated to understand the idea and concepts

Slide 48

Slide 48 text

Advantages of gRPC - Way smaller due to binary transfer - HTTP/2 by default - Simple client calls - Lighter and supports a lot of calls - Supports streaming both in client and server - Protobuf standardizes the message exchange

Slide 49

Slide 49 text

Comparison

Slide 50

Slide 50 text

refs_ - https://en.wikipedia.org/wiki/Remote_procedure_call - https://grpc.io/docs/what-is-grpc/introduction/ - https://developers.google.com/protocol-buffers - https://developers.google.com/protocol-buffers/docs/reference/javascript-generated - https://medium.com/xp-inc/introdu%C3%A7%C3%A3o-ao-grpc-com-node-js-98f6a4ede11 - https://grpc.io/docs/what-is-grpc/core-concepts/ - https://blog.logrocket.com/creating-a-crud-api-with-node-express-and-grpc/ - https://github.com/khaosdoctor/grpc-nodejs-demo-app

Slide 51

Slide 51 text

/khaosdoctor @_staticvoid lsantos.dev /khaosdoctor thank you_