A minimal polyglot app which uses gRPC for data communication between Node.js and Python
The repo contains three services -
- Node server service
/server
- Node client service
/client/node
- Python client service
/client/python
-
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
.
- In the first terminal type
-
Let's setup the node client
- In the second terminal type
$ cd client/node
. - Then do
$ npm i
.
- In the second terminal type
-
Let's setup the python client
- In the third terminal type
$ cd client/python
. - Then do
$ pipenv install
(If you don't havepipenv
install it with$ pip install pipenv
) - Run
$ pipenv run python gen_py.py
- In the third terminal type
- 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 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.
- The node server listens on localhost:50050
- Both the clients can call the server's two functions (sayHello & sayHelloAgain).
- 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.
- 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 :(