$30 off During Our Annual Pro Sale. View Details »

gRPC with Node.js

Lucas Santos
September 16, 2020

gRPC with Node.js

Lucas Santos

September 16, 2020
Tweet

More Decks by Lucas Santos

Other Decks in Programming

Transcript

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

    View Slide

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

    View Slide

  3. What is gRPC?

    View Slide

  4. 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

    View Slide

  5. View Slide

  6. RPC is NOT new

    View Slide

  7. 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

    View Slide

  8. Protocol Buffers
    protobuf for the bffs <3

    View Slide

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

    View Slide

  10. message.proto

    View Slide

  11. Compile time!

    View Slide

  12. Outfile

    View Slide

  13. message_pb.js

    View Slide

  14. Generated Methods

    View Slide

  15. Then we can just use

    View Slide

  16. gRPC !=
    Protobuf

    View Slide

  17. Protobuf is universal_

    View Slide

  18. View Slide

  19. gRPC is based on services_

    View Slide

  20. Defining a service

    View Slide

  21. Defining a service

    View Slide

  22. Types of methods_

    View Slide

  23. Unary

    View Slide

  24. Server Streaming

    View Slide

  25. Client Streaming

    View Slide

  26. Bidirectional

    View Slide

  27. Building the server
    with NodeJS

    View Slide

  28. Install grpc

    View Slide

  29. One small change

    View Slide

  30. Load the protofile

    View Slide

  31. Load the protofile
    "Database"

    View Slide

  32. Implement the functions on the serverside

    View Slide

  33. Implement the functions on the serverside

    View Slide

  34. Start the server

    View Slide

  35. Full code

    View Slide

  36. Full code
    load

    View Slide

  37. Full code
    load
    database

    View Slide

  38. Full code
    load
    database
    implementation

    View Slide

  39. Full code
    load
    database
    implementation
    run

    View Slide

  40. Creating the client

    View Slide

  41. Install grpc

    View Slide

  42. Load the protofile

    View Slide

  43. Add the service

    View Slide

  44. Call it!

    View Slide

  45. Full Code

    View Slide

  46. DEMO

    View Slide

  47. 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

    View Slide

  48. 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

    View Slide

  49. Comparison

    View Slide

  50. 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

    View Slide

  51. /khaosdoctor @_staticvoid
    lsantos.dev
    /khaosdoctor
    thank you_

    View Slide