grpc.client
v4.0.0
Published
a simple client for gRPC
Downloads
3
Readme
grpc.client
a simple NodeJS client for gRPC
api
const client = require('grpc.client')
- client(an optional {})
- type string, with the values
unary
andstream
,unary
is the default value - address string with the format
url:port
- credentials can use
credentials.createInsecure
or with certificates through an object { ca, key, client } and should be a Buffer - metadata set metadata that can be used in all calls, an array with { key: value }
- type string, with the values
methods
- service(service name - string, path to the protoFile - string)
- setMetadata(plain js object) to be used when needs to pass metadata to the server but the function doesn't send any data to the server
- end(err - Error object, res from the rpc server - depends of the proto/contract, metadata - plain js object)
grpc.client
will create dynamically the method or methods from the rpc methods list for a given service and protofile, check examples below
example
const client = require('grpc.client')
// proto file
/*
syntax = "proto3";
package helloWorld;
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc sayHello (HelloRequest) returns (HelloReply) {}
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}
*/
client()
.service('Greeter', protos.helloWorld)
.sayHello({ name: 'Scaramouche' })
.end((err, res, metadata) => {
if (err) {
// do something
}
console.log(res)// will print { message: 'Hello Scaramouche' }
})
//
// creating a static client
//
const greeter = client({ metadata: { xp: 'to' } })
greeter
.service('Greeter', protos.helloWorld)
.sayHello({ name: 'Scaramouche' }, { 'request-id': 12345 })
.end((err, res, metadata) => {
if (err) {
// do something
}
console.log(res)// will print { message: 'Hello Scaramouche' }
})
// no data sends to the server
greeter
.service('Greeter', protos.something)
.something()
.setMetadata({ 'request-id': 12345 })
.end((err, res, metadata) => {
//
})