grpc-kit
Use grpc more simply on Node.js.
quick start
install
$ npm install grpc @grpc/proto-loader grpc-kit
proto
syntax="proto3"; package greeter; service Greeter { rpc Hello (RequestGreet) returns (ResponseGreet) {} rpc Goodbye (RequestGreet) returns (ResponseGreet) {}} message RequestGreet { string name = 1;} message ResponseGreet { string message = 1;}
Server
const createServer = ;const server = ; server;server;
Client
const createClient = ;const client = ; client; client;
use stream
proto
syntax="proto3"; package stream_greeter; service StreamGreeter { rpc ClientStreamHello(stream Message) returns(Message) {} rpc ServerStreamHello(Message) returns(stream Message) {} rpc MutualStreamHello (stream Message) returns (stream Message) {}} message Message { string message = 1;}
Server
const createServer = ;const server = ; server; server;
Client
const createClient = ;const client = ;
client stream
const call = client;call;call;call;
server stream
const call = client;call;call;
mutual stream
const call = client;call;call;call;call;call;
api
createServer(): GrpcServer
Create GrpcServer
instance. GrpcServer
is a wrapper class of grpc.Server
providing simplified api to register services.
protoPath
,packageName
,serviceName
,options
,routes
}): GrpcServer
GrpcServer.use({Register a service to provide from a server.
arg name | type | required/optional | description |
---|---|---|---|
protoPath |
String | Required | path to .proto file |
packageName |
String | Required | name of package |
serviceName |
String | Required | name of service |
options |
@grpc/proto-loader.Options | Optional | options for @grpc/proto-loader to load .proto file. In detail, please check here out. Default is null |
routes |
{ [methodName]:(call, callback) => void | (call) => Promise } | Required | routing map consists of a set of pair of method name and handler. Both of sync function and async function are available as a handler |
address_port
, credentials
): GrpcServer
GrpcServer.listen(Start server. Alias to grpc.Server.bind()
and grpc.Server.start()
.
arg name | type | required/optional | description |
---|---|---|---|
address_port |
String | Required | address and port of server to listen |
credentials |
grpc.ServerCredentials | Optional | grpc server credentials. Default to insecure credentials generated by grpc.ServerCredentials.createInsecure() |
force
, callback
): GrpcServer
GrpcServer.close(Close server. Alias to grpc.Server.tryShutdown()
and grpc.Server.forceShutdown
.
arg name | type | required/optional | description |
---|---|---|---|
force |
Boolean | Optional | flag if force shutdown or not. Default to false |
callback |
()=>{} | Optional | call when shutdown completed. available only when force is false |
protoPath
,packageName
,serviceName
,options
},address_port
,credentials
): grpc.Client
createClient({Create grpc.Client
instance.
arg name | type | required/optional | description |
---|---|---|---|
protoPath |
String | Required | path to .proto file |
packageName |
String | Required | name of package |
serviceName |
String | Required | name of service |
options |
@grpc/proto-loader.Options | Optional | options for @grpc/proto-loader to load .proto file. In detail, please check here out. Default is null |
address_port |
String | Required | address and port of server to listen |
credentials |
grpc.ChannelCredentials | Optional | grpc channel credentials. Default to insecure credentials generated by grpc.credentials.createInsecure() |