Git Product home page Git Product logo

grpc-py-node's Introduction

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 :(

grpc-py-node's People

Contributors

itaditya avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.