Skip to content

Instantly share code, notes, and snippets.

@gistlyn
Created December 31, 2019 04:55
Show Gist options
  • Save gistlyn/05701a72bf35524fb897b81c1bac767b to your computer and use it in GitHub Desktop.
Save gistlyn/05701a72bf35524fb897b81c1bac767b to your computer and use it in GitHub Desktop.
Node.js Google protoc insecure GrpcServicesClient TodoWorld Example
const { Hello } = require('./services_pb.js');
const { GrpcServicesClient } = require('./services_grpc_pb.js');
const grpc = require('grpc');
const { promisify } = require('util');
async function main() {
const client = new GrpcServicesClient('todoworld.servicestack.net:5054',
grpc.credentials.createInsecure());
// Convert gRPC's callback APIs to await friendly promises
const { getHello } = promisifyAll(client);
let request = new Hello();
request.setName("gRPC Node.js");
let response = await getHello(request);
console.log(response.getResult());
}
function promisifyAll(client) {
const to = {};
for (var k in client) {
if (typeof client[k] != 'function') continue;
to[k] = promisify(client[k].bind(client));
}
return to;
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment