{"id":31667580,"url":"https://github.com/dongss/grpc-node-step","last_synced_at":"2025-10-08T00:13:28.877Z","repository":{"id":87695959,"uuid":"128706213","full_name":"Dongss/grpc-node-step","owner":"Dongss","description":"GRPC nodejs example","archived":false,"fork":false,"pushed_at":"2018-04-10T02:31:46.000Z","size":28,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-10T11:35:30.418Z","etag":null,"topics":["demo","example","grpc","nodejs"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Dongss.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-04-09T02:56:13.000Z","updated_at":"2018-04-10T02:31:47.000Z","dependencies_parsed_at":null,"dependency_job_id":"924b5671-b27f-4c9f-9e11-633478414c66","html_url":"https://github.com/Dongss/grpc-node-step","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Dongss/grpc-node-step","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Dongss%2Fgrpc-node-step","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Dongss%2Fgrpc-node-step/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Dongss%2Fgrpc-node-step/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Dongss%2Fgrpc-node-step/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Dongss","download_url":"https://codeload.github.com/Dongss/grpc-node-step/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Dongss%2Fgrpc-node-step/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":278867277,"owners_count":26059744,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-07T02:00:06.786Z","response_time":59,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["demo","example","grpc","nodejs"],"created_at":"2025-10-08T00:13:27.826Z","updated_at":"2025-10-08T00:13:28.869Z","avatar_url":"https://github.com/Dongss.png","language":"JavaScript","readme":"# grpc-node-step\n\n\u003c!-- START doctoc generated TOC please keep comment here to allow auto update --\u003e\n\u003c!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --\u003e\n\n- [Environment](#environment)\n- [Dynamic code generate](#dynamic-code-generate)\n  - [Step 1: Init files](#step-1-init-files)\n  - [Step 2: Specify named services](#step-2-specify-named-services)\n  - [Step 3: Server and client](#step-3-server-and-client)\n  - [Step 4: Run server and client](#step-4-run-server-and-client)\n- [Static code generate](#static-code-generate)\n  - [Step 1: Init files](#step-1-init-files-1)\n  - [Step 2: Specify named services](#step-2-specify-named-services-1)\n  - [Step 3: Compile proto to js file](#step-3-compile-proto-to-js-file)\n  - [Step 4: Server and client](#step-4-server-and-client)\n  - [Step 5: Run server and client](#step-5-run-server-and-client)\n- [Auth with SSL/TLS](#auth-with-ssltls)\n  - [Step 1: Init files](#step-1-init-files-2)\n  - [Step 2: Generate the certificates](#step-2-generate-the-certificates)\n  - [Step 3: Specify named services](#step-3-specify-named-services)\n  - [Step 4: Server and client](#step-4-server-and-client-1)\n  - [Step 5: Run server and client](#step-5-run-server-and-client-1)\n\n\u003c!-- END doctoc generated TOC please keep comment here to allow auto update --\u003e\n\n\n## Environment\n\n* `CentOS7`\n* `Node.JS` v8.10.0\n* [grpc](https://www.npmjs.com/package/grpc) v1.10.1 (npm)\n* [Protocol Buffers](https://github.com/google/protobuf) v3.5.1\n\n## Dynamic code generate\n\nCompile proto file to js dynamic, code is generated at runtime using [Protobuf.js](https://github.com/dcodeIO/ProtoBuf.js/)\n\n### Step 1: Init files\n\n```\nnpm install grpc --save\n```\n\nserver file: `./dynamic_codegen/server.js`\n\nclient file: `./dynamic_codegen/client.js`\n\nproto file: `./proto/demo/hello.proto`\n\n### Step 2: Specify named services\n\n[./proto/demo/hello.proto](./proto/demo/hello.proto):\n\n```\nsyntax = \"proto3\";\npackage hello;\n\n// The greeting service definition.\nservice Greeter {\n    // Sends a greeting\n    rpc SayHello (HelloRequest) returns (HelloReply) {}\n}\n\n// The request message containing the user's name.\nmessage HelloRequest {\n    string name = 1;\n}\n\n// The response message containing the greetings\nmessage HelloReply {\n    string message = 1;\n}\n```\n\n### Step 3: Server and client\n\n[./dynamic_codegen/server.js](./dynamic_codegen/server.js):\n\n```\nconst grpc = require('grpc');\n\nconst PROTO_PATH = __dirname + '/../proto/demo/hello.proto';\nconst HelloProto = grpc.load(PROTO_PATH).hello;\n\n/**\n * Implements the SayHello RPC method.\n */\nfunction sayHello(call, callback) {\n    callback(null, { message: 'Hello ' + call.request.name });\n}\n\nconst server = new grpc.Server();\nserver.addService(HelloProto.Greeter.service, {\n    sayHello: sayHello\n});\nserver.bind('0.0.0.0:9001', grpc.ServerCredentials.createInsecure());\nserver.start();\n```\n\n[./dynamic_codegen/client.js](./dynamic_codegen/client.js):\n\n```\nconst grpc = require('grpc');\nconst path = require('path');\n\nconst PROTO_PATH = __dirname + '/../proto/demo/hello.proto';\nconst HelloProto = grpc.load(PROTO_PATH).hello;\n\nconst client = new HelloProto.Greeter('localhost:9001', grpc.credentials.createInsecure());\n\nclient.sayHello({ name: 'World'}, function(err, response) {\n    console.log('Greeting res:', response.message);\n});\n```\n\n### Step 4: Run server and client\n\n`node .\\dynamic_codegen\\server.js`\n\nthen\n\n`node .\\dynamic_codegen\\client.js`\n\noutput: \n\n```\nGreeting res: Hello World\n```\n\n## Static code generate\n\nCode is pre-generated using protoc and the Node gRPC protoc plugin\n\n### Step 1: Init files\n\n```\nnpm install grpc google-protobuf --save\nnpm install grpc-tools --save-dev\n```\n\nserver file: `./static_codegen/server.js`\n\nclient file: `./static_codegen/client.js`\n\nproto file: `./proto/demo/hello.proto`\n\nproto and grpc compiled folder: `./static_codegen/proto_js/`\n\n### Step 2: Specify named services\n\n[./proto/demo/hello.proto](./proto/demo/hello.proto):\n\n```\nsyntax = \"proto3\";\npackage hello;\n\n// The greeting service definition.\nservice Greeter {\n    // Sends a greeting\n    rpc SayHello (HelloRequest) returns (HelloReply) {}\n}\n\n// The request message containing the user's name.\nmessage HelloRequest {\n    string name = 1;\n}\n\n// The response message containing the greetings\nmessage HelloReply {\n    string message = 1;\n}\n```\n\n### Step 3: Compile proto to js file\n\nCompile command:\n\n```\nprotoc --proto_path=./proto/ --js_out=import_style=commonjs,binary:./static_codegen/proto_js/ --grpc_out=./static_codegen/proto_js/ --plugin=protoc-gen-grpc=node_modules/grpc-tools/bin/grpc_node_plugin proto/demo/hello.proto\n```\n\n`./static_codegen/proto_js/` should be:\n\n```\nstatic_codegen/proto_js/\n└── demo\n    ├── hello_grpc_pb.js\n    └── hello_pb.js\n```\n\n### Step 4: Server and client\n\n[./static_codegen/server.js](./static_codegen/server.js):\n\n```\nconst grpc = require('grpc');\n\nconst HelloMessage = require('./proto_js/demo/hello_pb');\nconst HelloService = require('./proto_js/demo/hello_grpc_pb');\n\n/**\n * Implements the SayHello RPC method.\n */\nfunction sayHello(call, callback) {\n    let reply = new HelloMessage.HelloReply();\n\n    reply.setMessage( 'Hello ' + call.request.getName() );\n    callback(null, reply);\n}\n\nconst server = new grpc.Server();\nserver.addService(HelloService.GreeterService, {\n    sayHello: sayHello\n});\nserver.bind('0.0.0.0:9001', grpc.ServerCredentials.createInsecure());\nserver.start();\n```\n\n[./static_codegen/client.js](./static_codegen/client.js):\n\n```\nconst grpc = require('grpc');\n\nconst HelloMessage = require('./proto_js/demo/hello_pb');\nconst HelloService = require('./proto_js/demo/hello_grpc_pb');\n\nconst client = new HelloService.GreeterClient('localhost:9001', grpc.credentials.createInsecure());\n\nlet request = new HelloMessage.HelloRequest();\nrequest.setName('World');\n\nclient.sayHello(request, function(err, response) {\n    console.log('Greeting res:', response.getMessage());\n});\n```\n\n### Step 5: Run server and client\n\n`node .\\static_codegen\\server.js`\n\nthen\n\n`node .\\static_codegen\\client.js`\n\noutput: \n\n```\nGreeting res: Hello World\n```\n\n[google-protobuf js doc](https://github.com/google/protobuf/tree/master/js#api) The API is not well-documented yet. :sleeping: Not recommended for now.\n\n## Auth with SSL/TLS\n\n### Step 1: Init files\n\n```\nnpm install grpc --save\n```\n\nserver file: `./grpc_auth/server.js`\n\nclient file: `./grpc_auth/client.js`\n\nproto file: `./proto/demo/hello.proto`\n\ncertificates: `./keys/`\n\n### Step 2: Generate the certificates\n\nGenerate the certificates:\n\n```\nopenssl genrsa -passout pass:1111 -des3 -out ca.key 4096\nopenssl req -passin pass:1111 -new -x509 -days 365 -key ca.key -out ca.crt -subj  \"/C=FR/ST=Paris/L=Paris/O=Test/OU=Test/CN=ca\"\nopenssl genrsa -passout pass:1111 -des3 -out server.key 4096\nopenssl req -passin pass:1111 -new -key server.key -out server.csr -subj  \"/C=FR/ST=Paris/L=Paris/O=Test/OU=Server/CN=localhost\"\nopenssl x509 -req -passin pass:1111 -days 365 -in server.csr -CA ca.crt -CAkey ca.key -set_serial 01 -out server.crt\nopenssl rsa -passin pass:1111 -in server.key -out server.key\nopenssl genrsa -passout pass:1111 -des3 -out client.key 4096\nopenssl req -passin pass:1111 -new -key client.key -out client.csr -subj  \"/C=FR/ST=Paris/L=Paris/O=Test/OU=Client/CN=localhost\"\nopenssl x509 -passin pass:1111 -req -days 365 -in client.csr -CA ca.crt -CAkey ca.key -set_serial 01 -out client.crt\nopenssl rsa -passin pass:1111 -in client.key -out client.key\n\n```\n\n./keys/ :\n\n```\nkeys/\n├── ca.crt\n├── ca.key\n├── client.crt\n├── client.csr\n├── client.key\n├── server.crt\n├── server.csr\n└── server.key\n```\n\n### Step 3: Specify named services\n\n[./proto/demo/hello.proto](./proto/demo/hello.proto):\n\n```\nsyntax = \"proto3\";\npackage hello;\n\n// The greeting service definition.\nservice Greeter {\n    // Sends a greeting\n    rpc SayHello (HelloRequest) returns (HelloReply) {}\n}\n\n// The request message containing the user's name.\nmessage HelloRequest {\n    string name = 1;\n}\n\n// The response message containing the greetings\nmessage HelloReply {\n    string message = 1;\n}\n```\n\n### Step 4: Server and client\n\n[./grpc_auth/server.js](./grpc_auth/server.js):\n\n```\nconst fs = require('fs');\nconst grpc = require('grpc');\n\nconst PROTO_PATH = __dirname + '/../proto/demo/hello.proto';\nconst HelloProto = grpc.load(PROTO_PATH).hello;\n\n/**\n * Implements the SayHello RPC method.\n */\nfunction sayHello(call, callback) {\n    callback(null, { message: 'Hello ' + call.request.name });\n}\n\nconst server = new grpc.Server();\nserver.addService(HelloProto.Greeter.service, {\n    sayHello: sayHello\n});\nserver.bind('0.0.0.0:9001', grpc.ServerCredentials.createSsl(\n    fs.readFileSync('./keys/ca.crt'),  [\n    {\n        private_key: fs.readFileSync('./keys/server.key'),\n        cert_chain: fs.readFileSync('./keys/server.crt')\n    }\n], true));\nserver.start();\n```\n\n[./grpc_auth/client.js](./grpc_auth/client.js):\n\n```\nconst grpc = require('grpc');\nconst fs = require('fs');\n\nconst PROTO_PATH = __dirname + '/../proto/demo/hello.proto';\nconst HelloProto = grpc.load(PROTO_PATH).hello;\n\nconst client = new HelloProto.Greeter('localhost:9001', grpc.credentials.createSsl(\n    fs.readFileSync('./keys/ca.crt'),  \n    fs.readFileSync('./keys/client.key'),\n    fs.readFileSync('./keys/client.crt'),\n));\n\nclient.sayHello({ name: 'World'}, function(err, response) {\n    console.log(err);\n    console.log('Greeting res:', response.message);\n});\n```\n\n### Step 5: Run server and client\n\n`node .\\grpc_auth\\server.js`\n\nthen\n\n`node .\\grpc_auth\\client.js`\n\noutput: \n\n```\nnull\nGreeting res: Hello World\n```\n\nIf without certificates:\n\n```\nconst client = new HelloProto.Greeter('localhost:9001', grpc.credentials.createSsl(\n    // fs.readFileSync('./keys/ca.crt'),  \n    // fs.readFileSync('./keys/client.key'),\n    // fs.readFileSync('./keys/client.crt'),\n));\n```\n\noutput should be:\n\n```\nE0409 10:22:29.239000000 11968 ssl_transport_security.cc:989] Handshake failed with fatal error SSL_ERROR_SSL: error:1000007d:SSL routines:OPENSSL_internal:CERTIFICATE_VERIFY_FAILED.\nE0409 10:22:29.259000000 11968 ssl_transport_security.cc:989] Handshake failed with fatal error SSL_ERROR_SSL: error:1000007d:SSL routines:OPENSSL_internal:CERTIFICATE_VERIFY_FAILED.\n{ Error: 14 UNAVAILABLE: Connect Failed\n    at new createStatusError \n    ...\n  code: 14,\n  metadata: Metadata { _internal_repr: {} },\n  details: 'Connect Failed' }\n```","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdongss%2Fgrpc-node-step","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdongss%2Fgrpc-node-step","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdongss%2Fgrpc-node-step/lists"}