Nodejs App how to call a grpc Server

Viewed 1742

I'm trying to figure out how can I call a grpc server from a javascript function in my nodejs application, and update my page... I don't know how can I manage it.

This is the proto file:

syntax = "proto3";

package helloworld;

service Greeter {
  rpc SayHello (HelloRequest) returns (HelloReply) {}
}

message HelloRequest {
  string name = 1;
}

message HelloReply {
  string message = 1;
}

this is the server:

var PROTO_PATH = __dirname + '/helloworld.proto';

var grpc = require('grpc');
var hello_proto = grpc.load(PROTO_PATH).helloworld;

function sayHello(call, callback) {
    callback(null, {message: 'Hello ' + call.request.name});
}

function main() {
    var server = new grpc.Server();
    server.addProtoService(hello_proto.Greeter.service, {sayHello: sayHello});
    server.bind('0.0.0.0:50051', grpc.ServerCredentials.createInsecure());
    server.start();
}

main();

this is the javascript function :

var PROTO_PATH = __dirname + '/helloworld.proto';

var grpc = require('grpc');
var hello_proto = grpc.load(PROTO_PATH).helloworld;

function test() {
    var client = new hello_proto.Greeter('localhost:50051',
        grpc.credentials.createInsecure());

    client.sayHello(function(err, response) {
        console.log('Greeting:', response.message);
        document.getElementById("para").innerHTML = 'Mex Received from grpc server ' + response.message;
    });
}

when I click a button on my page i call the function test and i would like to update the element(id=para).

Any idea?

0 Answers
Related