Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gRPC private channels

Tags:

go

grpc

Is it possible to only stream to certain clients from a gRPC server?

I believe what I'm looking for is something like Pusher, where you have a channel for a client and you can publish messages that can be seen only by a client that has access to that channel.

What I'm struggling with is understanding what are the steps we need to take to do something like this.

Thinking about web-sockets I believe we can store each client connection, then we can find that connection and send messages. How can we do a similar thing with gRPC?

like image 382
user2502761 Avatar asked Mar 12 '18 08:03

user2502761


People also ask

Are gRPC calls blocking?

gRPC supports non-blocking client calls. What this means is that when the client makes a call to the service, it does not need to wait for the server response. To handle the server response, the client can simply pass in the observer which dictates what to do when the response is received.

How do I secure my gRPC call?

Generate TLS certificates First, generate CA's private key and its self-signed certificate. Second, create web server's private key and CSR. And third, use CA's private key to sign the web server's CSR and get back its certificate.

Does gRPC need TLS?

gRPC is designed to work with a variety of authentication mechanisms, making it easy to safely use gRPC to talk to other systems. You can use our supported mechanisms - SSL/TLS with or without Google token-based authentication - or you can plug in your own authentication system by extending our provided code.

What is a gRPC channel?

A gRPC channel provides a connection to a gRPC server on a specified host and port. It is used when creating a client stub. Clients can specify channel arguments to modify gRPC's default behavior, such as switching message compression on or off. A channel has state, including connected and idle .


1 Answers

As per as i understood the question. You want to send the the message to the particular client in gRPC. This is very much possible using Server side streaming or Bi-directional streaming in gRPC.

For example:

Define a server side streaming or bidi streaming api

rpc ListFeatures(Rectangle) returns (stream Feature) {}

On Server side:

func ListFeatures(rect *pb.Rectangle, stream pb.RouteGuide_ListFeaturesServer) error {
  // Save this stream instance in the server on a map or other suitable data structure
  // so that you can query for this stream instance later
  // This will act same like your websocket session
}

When you want to send something to a specific client then get the stream instance and do

err := stream.Send(feature); // Any times as required

On the client, it will be waiting for messages like this

stream, err := client.ListFeatures(ctx, rect)

for {
    feature, err := stream.Recv()
    ...
    // handle message here
}

Same thing can be done for bidi streaming rpc also. I hope this answers your question

like image 170
Shettyh Avatar answered Sep 27 '22 00:09

Shettyh