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
?
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.
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.
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.
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 .
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With