Skip to content

Instantly share code, notes, and snippets.

@gistlyn
Created December 31, 2019 04:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gistlyn/5060fffce784ae7d2ea177492e7ddfa6 to your computer and use it in GitHub Desktop.
Save gistlyn/5060fffce784ae7d2ea177492e7ddfa6 to your computer and use it in GitHub Desktop.
Node.js Google protoc SSL GrpcServicesClient TodoWorld Example
const { Hello } = require('./services_pb.js');
const { GrpcServicesClient } = require('./services_grpc_pb.js');
const grpc = require('grpc');
const { promisify } = require('util');
const fs = require('fs');
async function main() {
const client = new GrpcServicesClient('todoworld.servicestack.net:50051',
grpc.credentials.createSsl(fs.readFileSync('grpc.crt')));
// 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