Interceptors, Deadlines, and Metadata
Add cross-cutting auth and logging via interceptors while enforcing deadlines and passing metadata.
Cross-Cutting Concerns in gRPC
Once your gRPC service grows, you'll repeat the same logic in every handler: authentication, logging, timing, error shaping. Copy-pasting that into each method is fragile.
gRPC gives you three tools to handle this cleanly:
- Interceptors — middleware that wraps every call (client or server side).
- Metadata — key/value headers that travel alongside the request, perfect for auth tokens and request IDs.
- Deadlines — an absolute time by which a call must complete, propagated across services.
In this lesson we'll wire all three together with @grpc/grpc-js in Node.js.
What Metadata Actually Is
Metadata is a multimap of string keys to values sent with a gRPC call, similar to HTTP headers. Keys are case-insensitive. Values are usually ASCII strings; keys ending in -bin carry binary Buffer values.
On the client you attach metadata; on the server you read it from the call object. Use it for things that aren't part of the business payload — auth tokens, trace IDs, locale.
const grpc = require('@grpc/grpc-js');
// Build metadata on the client
const md = new grpc.Metadata();
md.set('authorization', 'Bearer abc123');
md.set('x-request-id', 'req-42');
// Reading is case-insensitive
console.log(md.get('Authorization')); // [ 'Bearer abc123' ]
console.log(md.get('x-request-id')); // [ 'req-42' ]All lessons in this course
- Defining Services and Messages with Protobuf IDL
- Unary, Server, Client, and Bidirectional Streaming RPCs
- Interceptors, Deadlines, and Metadata
- Proto Evolution and Backward Compatibility