grpc-promise
Table of contents
gRPC?
What'sHigh performance, open source, general-purpose RPC framework.
Calling service types
- Simple RPC: one single asynchronous call
- Streaming RPC: one stream is used for the call
Implementations
- Unary Request: One single message request, one single message response
- Client Stream Request: One Writable stream message request, one single message response, sent once the stream is closed
- Server Stream Request: One single message request, one Readable stream message response
- Bidirectional Stream Request: One Duplex stream for request and response
Introduction to grpc-promise
GRPC promisify module for all Request/Response types: standard and stream.
This module unifies the way the grpc are implemented in NodeJS.
Depending on which kind of RPC call is implemented (simple vs streaming), we need to manage communication differently:
- Simple RPC: sending message and setting callbacks for responses
- Streaming RPC: opening stream and managing stream events to write and read messages
The goal of grpc-promise is to standardize all kind of calls, whatever they are, from the client side.
Installation
$ npm install grpc-promise
API by example
- Test proto file test.proto
- Complete GRPC Server implementation server.js
- All kind of GRPC Client implementations:
- Unary Request Example client-unary.js
- Client Stream Request Example client-client-stream.js
- Server Stream Request Example client-server-stream.js
- Bidirectional Stream Request Example client-bidi-stream.js
Proto example file
Let's take a simple proto file exposing all kind of requests:
syntax = "proto3"; package test; service Test message TestRequest message TestResponse
Unary Request
Implementation of TestSimpleSimple
message
Server side:
const grpc = ;const protoLoader = ; const packageDefinition = protoLoader; const protoDescriptor = grpc;const test_proto = protoDescriptortest; const testSimpleSimple = { console; // Server: Simple Message Received = {id: 1} ;}; { server = ; server; server; serverstart;} ;
Client side:
const grpc = ;const protoLoader = ;const grpc_promise = ;; const packageDefinition = protoLoader; const protoDescriptor = grpc;const test_proto = protoDescriptortest; { const client = 'localhost:50052' grpccredentials; grpc_promise; client ;} ;
Client Stream Request
Implementation of TestStreamSimple
message
Server side:
const grpc = ;const protoLoader = ; const packageDefinition = protoLoader; const protoDescriptor = grpc;const test_proto = protoDescriptortest; const testStreamSimple = { var messages = ; call; call;}; { server = ; server; server; serverstart;} ;
Client side:
const grpc = ;const protoLoader = ;const grpc_promise = ; const packageDefinition = protoLoader; const protoDescriptor = grpc;const test_proto = protoDescriptortest; { const client = 'localhost:50052' grpccredentials; grpc_promise; client ;} ;
Server Stream Request
Implementation of TestSimpleStream
message
Server side:
const grpc = ;const protoLoader = ; const packageDefinition = protoLoader; const protoDescriptor = grpc;const test_proto = protoDescriptortest; const testSimpleStream = { console; // Server: Simple Message Received = {id: 1} call; call; call;}; { server = ; server; server; serverstart;} ;
Client side:
const grpc = ;const protoLoader = ;const grpc_promise = ; const packageDefinition = protoLoader; const protoDescriptor = grpc;const test_proto = protoDescriptortest; { const client = 'localhost:50052' grpccredentials; grpc_promise; client ;} ;
Bidirectional Stream Request
Implementation of TestStreamStream
message
IMPORTANT: In order to keep track of the messages sent and to be able to callback all the requests, the server implementation needs to answer with the same id received
Server side:
const grpc = ;const protoLoader = ; const packageDefinition = protoLoader; const protoDescriptor = grpc;const test_proto = protoDescriptortest; const testStreamStream = { call; call;}; { server = ; server; server; serverstart;} ;
Client side:
const grpc = ;const protoLoader = ;const grpc_promise = ; const packageDefinition = protoLoader; const protoDescriptor = grpc;const test_proto = protoDescriptortest; { const client = 'localhost:50052' grpccredentials; // Optional `timeout_message` out definition (defaults = 50) // for the response of a single message of a bidirectional stream function in grpc. // Don't confuse with `timeout` parameter used to set a deadline to the open connection. grpc_promise; t = client; t ; t ; t;} ;
Request with Metadata
Client side:
const grpc = ;const protoLoader = ;const grpc_promise = ; const packageDefinition = protoLoader; const protoDescriptor = grpc;const test_proto = protoDescriptortest; { const client = 'localhost:50052' grpccredentials; const meta = ; meta; grpc_promise; client ;} ;
Using Deadline
The deadline parameter is used to to set a timestamp in millisenconds for the entire call to complete. For architectural reasons and knowing that the original deadline is passed as a timestamp, grpc-module forces to set a timeout parameter in milliseconds. It means the number of milliseconds we want to wait as most for the response:
// We give a deadline of 1 second (= 1000ms)grpc_promise;
Promisify single function
Client side:
const grpc = ;const grpc_promise = ;const test_proto = grpctest; { const client = 'localhost:50052' grpccredentials; const meta = ; meta; grpc_promise; client ;} ;
MIT License
Copyright (c) 2017 Carles Sistare
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.