Skip to content

itaditya/grpc-py-node

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gRPC with Node and Python

A minimal polyglot app which uses gRPC for data communication between Node.js and Python

The repo contains three services -

  1. Node server service /server
  2. Node client service /client/node
  3. Python client service /client/python

Project Setup

  • Clone the repo and cd into it

  • We have to setup the three services one by one, so open three separate terminals from root dir.

  • Let's setup the node server:

    • In the first terminal type $ cd server/.
    • Then do $ npm i.
  • Let's setup the node client

    • In the second terminal type $ cd client/node.
    • Then do $ npm i.
  • Let's setup the python client

    • In the third terminal type $ cd client/python.
    • Then do $ pipenv install (If you don't have pipenv install it with $ pip install pipenv)
    • Run $ pipenv run python gen_py.py

Starting the Services

  • In the first terminal (node server) run $ node index.js.
  • In the second terminal (node client) run $ node index.js.
  • In the third terminal (python client) run $pipenv run python app.py.

gRPC Explanation

  • gRPC is Google's project which allows us to call functions from one service to another. Awesome right!!
  • These services can be written in entirely different languages.
  • We make .proto files to define all the services(functions) and messages(data schemas).
  • The .proto file is machine readable due to which native clients can be autogenerated for each language.
  • To sum all this, the client can trigger functions specified in .proto files which gets called in the server and the result of the function can be returned back to the client.

What our app is doing?

  • The node server listens on localhost:50050
  • Both the clients can call the server's two functions (sayHello & sayHelloAgain).

Gotcha's

  • You may notice the three additonal files in the client/python directory. Well, what are they?
  • In certain languages we have to build an intermediate native client to communicate with the server.
  • In case of REST, we manually have to do it for all languages.(Think import pydrive for Google Drive API for Python)
  • In gRPC, this can be automated. To do this we used $ pipenv run python gen_py.py for python.
  • This command creates two files helloworld_pb2.py & helloworld_pb2_grpc.py
  • Whenever we make changes in the helloworld.proto, or add new files we have to execute the cmd.

Note-- that the node client didn't need this intermediary. However if we add more clients (in different lang) then for them we may have to generate the native client.

Comparison with REST

  • In REST we specify URLs to perform particular task like /users/1, in gRPC we use services.
  • When we send POST request we send some JSON data, this data needs to match the desired schema. If for example we didn't sent the id of user then this will cause error. To check whether the JSON data is valid or not we do manual checks. In gRPC this data is specified in the .proto files messages.
  • Although the API is available to everyone via Internet, so we can make http requests to transfer data, it is mostly cubersome, repetitive. So API vendors provide custom native clients for each language to ease this. However this has to be done manually for each language. In gRPC this is done automatically.
  • In REST we use JSON or XML both of which although human readable are large in size. gRPC uses protobufs which is a binary format, so it's smaller and faster.
  • REST APIs can be used by browsers. gRPC doesn't support browsers yet :(

About

A minimal polyglot app which uses gRPC for data communication between Node.js and Python

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published