Grpc-node: How to edit metadata and send back edited metadata to client?
Below is currently what I have at the moment, and it seems like the metadata can be passed to the grpc-client RPC method ('greetmath'), but I can't edit the metadata in the server and send it back to the client. It can only send back metadata that was originally created.
Is there anyway I can edit the metadata in the grpc server and send that edited metadata to the grpc client?
Greeter_server.js
const path = require('path');
const PROTO_PATH = path.join(__dirname, '../proto/greet.proto');
// console.log("Proto path: ", PROTO_PATH);
const protoLoader = require('@grpc/proto-loader') //require('@grpc/proto-loader')
const grpc = require('grpc')
//grpc service definition for greet
const greetProtoDefinition = protoLoader.loadSync(PROTO_PATH, {
keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true
});
const greetPackageDefinition = grpc.loadPackageDefinition(greetProtoDefinition).greet
function greetFunc(call, callback) {
var firstName = call.request.greeting.first_name;
var lastName = call.request.greeting.last_name;
callback(null, {result: "Hello " + firstName + " " + lastName});
}
function greetMath(call, callback) {
console.log("callback: ", call);
console.log("Meta data: ", call.metadata._internal_repr.somekey);
call.metadata._internal_repr.somekey.push('random');
var firstName = call.request.greeting.first_name;
var lastName = call.request.greeting.last_name;
let current = Number(process.hrtime.bigint());
console.log("call obj: ", call);
console.log("callback obj: ", callback);
callback(null, {result: "Hello " + firstName + " " + lastName + " current: " + current});
}
function main() {
const server = new grpc.Server()
server.addService(greetPackageDefinition.GreetService.service, {
greet: greetFunc,
greetMath: greetMath
});
server.bind("127.0.0.1:4000", grpc.ServerCredentials.createInsecure());
server.start();
console.log("Server Running at http://127.0.0.1:50051")
}
main()
greet.proto
syntax = "proto3";
package greet;
service GreetService {
//Unary API
rpc Greet (GreetRequest) returns (GreetResponse) {};
rpc GreetMath(GreetRequest) returns (GreetResponse) {};
}
message Greeting {
string first_name = 1;
string last_name = 2;
}
message GreetRequest {
Greeting greeting = 1;
}
message GreetResponse {
string result = 1;
}
greeter_client.js
const path = require('path');
const PROTO_PATH = path.join(__dirname, '../proto/greet.proto');
const protoLoader = require('@grpc/proto-loader') //require('@grpc/proto-loader')
const grpc = require('grpc')
//grpc service definition for greet
const greetProtoDefinition = protoLoader.loadSync(PROTO_PATH, {
keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true
});
const greetPackageDefinition = grpc.loadPackageDefinition(greetProtoDefinition).greet
const client = new greetPackageDefinition.GreetService("localhost:4000",
grpc.credentials.createInsecure()
)
function callGreetings() {
var request = {
greeting: {
first_name: "Jerry",
last_name: "Tom"
}
}
client.greet(request, (error, response) => {
if(!error) {
console.log("Greeting Response: ", response.result);
} else {
console.error(error)
}
})
}
function callGreetingsLogger() {
var request = {
greeting: {
first_name: "Jerry",
last_name: "Tom"
}
}
let meta = new grpc.Metadata();
meta.add('someKey', 'someVal');
let end;
let start = Number(process.hrtime.bigint());
client.greetMath(request, meta, (error, response) => {
if(!error) {
console.log("res: ", response);
console.log("metadata: ", meta);
console.log("Greeting Response: ", response.result);
end = Number(process.hrtime.bigint());
console.log("start: ", start);
console.log("end: ", end);
console.log("end - start", (end - start)/ 1000000, "ms");
function getDateTime() {
var date = new Date();
var hour = date.getHours();
hour = (hour < 10 ? "0" : "") + hour;
var min = date.getMinutes();
min = (min < 10 ? "0" : "") + min;
var sec = date.getSeconds(); sec = (sec < 10 ? "0" : "") + sec;
var year = date.getFullYear();
var month = date.getMonth() + 1; month = (month < 10 ? "0" : "") + month;
var day = date.getDate(); day = (day < 10 ? "0" : "") + day;
return month + "/" + day + "/" + year + " | Time: " + hour + ":" + min + ":" + sec;
}
let currentTime = getDateTime();
console.log("Current time: ", currentTime);
} else {
console.error(error)
}
});
}
function main() {
callGreetings();
callGreetingsLogger();
}
main()