1
votes

From the client side, it is easy to add metadata for the server:

const meta = new grpc.Metadata();
meta.add('xyz', 'okay');

stub.service.Rpc(request, meta, (err, response) => {
});

The above can be accessed on the server like this:

call.metadata.get('xyz');

Now, if we need to send metadata from the server to the client, we do this:

const err = { code, details };
const meta = new grpc.Metadata();
meta.add('...', '...');

callback(err, null, meta);

Note that we are passing error, and the actual response is null.

How do I pass a null error and a non-null response, along with metadata?

If I do the following, it does not seem to work as there is no way to access the metadata on the client without the error.

callback(null, r, meta);
// `r` is some response message

Does gRPC spec explicitly disallow sending metadata from server to client when there is no error?

Also, while we're at it, I'd like someone explain how do we send trailing vs initial metadata from server to client in Node.

Relevant links:

2

2 Answers

2
votes

ServerUnaryCall.sendMetadata(responseMetadata)

server:

const method = (call, cb) => {
  // code
  call.sendMetadata(metadata)
  // code
}

client:

const call = client.method(params, cb)
call.on('metadata', (metadata) => {
  // code
})
0
votes

Looks like you can use such code:

client.someFunction().on('metadata', (meta) => { /* any code */ })

At least on v0.9.x you can see: https://github.com/grpc/grpc-node/blob/v1.9.x/packages/grpc-native-core/src/client.js#L562