GithubHelp home page GithubHelp logo

basemax / firstnodejsgrpc Goto Github PK

View Code? Open in Web Editor NEW
8.0 2.0 1.0 95 KB

A example repository to show how you can create your first gRPC webservice (+ Protobuf in HTTP2 protocol) with NodeJS.

Home Page: https://grpc.io

License: GNU General Public License v3.0

JavaScript 100.00%
grpc grpc-node grpc-nodejs grpc-client grpc-server grpc-gateway grpc-service bloomrpc protobuf protobuf3 protobuffer protobuf-nodejs nginx nginx-grpc grpc-nginx

firstnodejsgrpc's Introduction

First NodeJS gRPC

A example repository to show how you can create your first gRPC webservice (+ Protobuf in HTTP2 protocol) with NodeJS.

What you already need to know before this repository

  • HTTP/2: HTTP/2 (or h2) is a binary protocol that brings push, multiplexing streams and frame control to the web.
  • Protobuf: Protocol Buffers (a.k.a., protobuf) are Google's language-neutral, platform-neutral, extensible mechanism for serializing structured data. Protobuf is an Interface Definition Language that is language-neutral and platform-neutral. ... With protobuf, you define your message format in a .proto file. You can then use the protobuf compilers to generate the client and service side code to encode and parse the data.
  • gRPC: gRPC is a modern open source high performance Remote Procedure Call (RPC) framework that can run in any environment. It can efficiently connect to services.

Purposes

  • HTTP/2 (and not HTTP/1)
  • Protobuf (and not JSON)
  • gRPC (and not REST)

Why Protobuf is much optimized and faster?

Take a fast look at the output of each structure and its bytes size:

JSON (9 Bytes):

{
  "id":42
}

Protobuf (2 Bytes):

0x08 0x2a

Getting started

rm helloworld_grpc_pb.js
rm helloworld_pb.js 
sudo npm install -g grpc-tools
grpc_tools_node_protoc --js_out=import_style=commonjs,binary:. --grpc_out=grpc_js:. helloworld.proto

Start the server

node server.js

Start the client to test

node client.js

Test with playground software (BloomRPC)

BloomRPC - first nodejs grpc

Structure

Protobuf structure:

message HelloRequest {
  string name = 1;
}

message HelloReply {
  string message = 1;
  int32 age = 2;
}

Connect gRPC to Nginx

server {
  listen 94 http2;
  location / {
    # Replace localhost:50051 with the address and port of your gRPC server
    # The 'grpc://' prefix is optional; unencrypted gRPC is the default
    grpc_pass grpc://localhost:50051;
  }
}

And restart nginx:

sudo systemctl restart nginx

Read more: https://www.nginx.com/blog/nginx-1-13-10-grpc/

Benchmark Performance

Using https://ghz.sh/:

$ ghz --insecure --proto ./helloworld.proto --call helloworld.Greeter.SayHello -d '{"name":"Max"}' 0.0.0.0:50051

Summary:
  Count:	200
  Total:	35.95 ms
  Slowest:	13.22 ms
  Fastest:	2.34 ms
  Average:	8.63 ms
  Requests/sec:	5562.82

Response time histogram:
  2.337 [1]	|∎
  3.426 [26]	|∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎
  4.514 [0]	|
  5.602 [6]	|∎∎∎∎∎∎
  6.691 [19]	|∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎
  7.779 [31]	|∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎
  8.868 [40]	|∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎
  9.956 [0]	|
  11.044 [10]	|∎∎∎∎∎∎∎∎∎∎
  12.133 [26]	|∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎
  13.221 [41]	|∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎

Latency distribution:
  10 % in 2.88 ms 
  25 % in 5.73 ms 
  50 % in 8.42 ms 
  75 % in 11.98 ms 
  90 % in 13.06 ms 
  95 % in 13.16 ms 
  99 % in 13.20 ms 

Status code distribution:
  [OK]   200 responses   

Minimum:

Average:	16.03 ms
Requests/sec:	3062.49

Maximum:

Average:	8.63 ms
Requests/sec:	5562.82

Minimum with Nginx:

Average:	26.05 ms
Requests/sec:	1870.37

Maximum with Nginx:

Average:	10.59 ms
Requests/sec:	4606.05

Note: These tests were not performed under specific and appropriate conditions. It is better to try it yourself.

Hardware

  • CPU: Intel Core i5-9400F @ 6x 4.1GHz [52.0°C]
  • Node: v13.14.0
  • NPM: 6.14.4

Read more:

© Copyright Max Base, 2021

firstnodejsgrpc's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

cavr

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.