Skip to content

Instantly share code, notes, and snippets.

@gistlyn

gistlyn/index.js

Created Dec 31, 2019
Embed
What would you like to do?
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
You can’t perform that action at this time.