0

I have a gRPC service running like this:

server.addService(PassportService, implementation);
server.bind(mfeConfig().grpc.passport, grpc.ServerCredentials.createInsecure());
server.start();

I can call my service from a client like this:

const request = new GetConsoleUserRequest();
const meta = new grpc.Metadata();
meta.add('__.grpc.exchanged-token', token);
this.client.getConsoleUser(
  request,
  meta,
  (err: grpc.ServiceError, val: GetConsoleUserResponse) => {

But I can't figure out how to read the metadata in my service implementation. Now matter which overloads I define for getConsoleUser, the metadata is never one of the arguments. Here's getConsoleUser, which just returns some fake data:

getConsoleUser: (_req: GetConsoleUserRequest, callback: Function) => {
  const response = new GetConsoleUserResponse();
  const user = new ConsoleUser();
  user.setName('Bob Loblaw');
  // Change me to userStatus.GUEST to simulate anonymous user access
  user.setState(userStatus.REGISTERED);
  user.setEmail('bob@loblaw.com');
  response.setConsoleUser(user);
  callback(null, response);
},

I've tried changing that signature to this:

getConsoleUser: (_req: GetConsoleUserRequest, meta: grpc.Metadata, callback: Function) => {

But if I do that, the second argument is actually the callback function, and the third argument is undefined.

Is there a way to read the metadata from my service implementation? Or is there some other class that I have to attach my service to so that I can listen for incoming metadata?

Samo
  • 8,202
  • 13
  • 58
  • 95

1 Answers1

2

The first argument passed to the server method is not a message object. It is a "call" object that has multiple different properties, including call.metadata to get the metadata, and for unary and server streaming methods, call.request has the actual request message. For streaming requests the call object is also a Node.js Stream object that you can write to and/or read from, whichever is relevant.

For more details, look at the ServerUnaryCall and Server(Readable|Writable|Duplex)Stream classes in the API reference documentation.

murgatroid99
  • 19,007
  • 10
  • 60
  • 95