Is there a way to get the connection information on RPC calls from server side? Or maybe something like unique client ID?
-
Depending on the programming language with which you are working this might be possible today or "coming soon". With what programming language(s) are you working with gRPC? – Nathaniel Manista At Google Jan 11 '17 at 15:30
-
I'm using C++ and Rust (unofficial Rust port https://github.com/stepancheg/grpc-rust , I don't think they support it yet) – Kibeom Kim Jan 11 '17 at 18:52
-
What do you want to achieve? Designs using a client ID are typically broken. – Eric Anderson Feb 06 '17 at 19:17
-
It's a game api, and say, if client1 calls `Jump()` then server should know which player in the game should jump. – Kibeom Kim Feb 06 '17 at 23:16
-
@KibeomKim man were you able to find a solution to distinguish clients ? – Venelin Sep 29 '20 at 12:01
4 Answers
There is no connecton information which may help distinguish clients. One reason of this is proxies: different clients can have same IP and port (as I understand)
One possible solution is handshake protocol in app level. You can add rpc method "Connect" and send clientId as response from server. Afterthat you can attach custom headers (metadata) to your rpc calls.
Client side java code:
String clientId = getIdfromServer();
Metadata.Key<String> CLIENT_ID = Metadata.Key.of("client_id", ASCII_STRING_MARSHALLER);
Metadata fixedHeaders = new Metadata();
fixedHeaders.put(CLIENT_ID, clientId);
blockingStub = MetadataUtils.attachHeaders(blockingStub, fixedHeaders);
This C++ server side code shows how to handle such header on server:
::grpc::Status YourRPC(::grpc::ServerContext* context, const Your* request, YourResponse* response)
{
const auto clientMetadata = context->client_metadata();
auto it = clientMetadata.find("client_id");
auto clientId = std::string(it->second.begin(), it->second.end());
}
I noticed that metadata key is case insensitive. Grpc converts keys to lowercase.

- 300
- 2
- 6
gRPC now provide peer information (https://github.com/grpc/grpc-go/issues/334)
import ( "google.golang.org/grpc/peer" )
func (s *server) Hello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
//p includes connection information
p, ok := peer.FromContext(ctx)
....
....
}
Yes , we can get the request information , connection information and etc. There are generally two types of information we can get from the client request on grpc server side.
Method Information : We know that rpc call is simple method call . To get the method name (ie : which method will be invoked in grpc server when client will request?). below code will work.
import ( "google.golang.org/grpc" ) func getMethodInfo(ctx context.Context) { methodName := grpc.Method(ctx) fmt.Println(methodName) } //outputex: /Abc
2.Peer Information:
p, ok := peer.FromContext(ctx)
Hope this will work.

- 21
- 1
For gRPC streaming
We can get the connection information below through google.golang.org/grpc/peer
// protobuf
service Server {
rpc Stream (stream GrpcStreamRequest) returns (stream GrpcStreamResponse) {}
}
func (ss *StreamServer) Stream(svr pb.Server_StreamServer) error {
for {
req, err := svr.Recv()
if err != nil {
fmt.Printf("Stream recv err %+v", err)
return err
}
p, ok := peer.FromContext(svr.Context())
if ok {
fmt.Printf("Client info %+v", p)
}
}

- 43,869
- 19
- 177
- 214