c# client side streaming

29 views
Skip to first unread message

Sam

unread,
Jun 26, 2019, 4:59:22 AM6/26/19
to grpc.io
Hi all, 

I’m using gRPC’s client-side streaming functionality (C#) to send many messages to a server. Most examples I can see on the web show a certain amount of messages sent and then read a reply. Like the SumExample in C#:

using (var call = client.Sum())
{
    await call
.RequestStream.WriteAllAsync(numbers);
   
Console.WriteLine("Sum Result: " + await call.ResponseAsync);
}


I use the client-side streaming in a slightly different way: a long lived stream to the server. I have multiple threads sending to the server and I use it like this:

I have a Connection class that wraps the gRPC client, the implementation goes something like this:

public class Connection
{
   
private readonly IClientStreamWriter<Request> _clientSideStream;
 
   
public Connection(RequestService.RequestServiceClient client)
   
{
       
// get a reference to the stream
       _clientSideStream
= client.TransactionExchangeClientStream().RequestStream;
   
}
 
   
// called a lot by multiple threads
   
public async Task SendAsync(Request request)
   {
       await _clientSideStream
.WriteAsync(request);
   }


   
// when the app decides to stop talking entirely to the server
   
public async Task CloseConnection()
   
{
       await _
clientSideStream.CompleteAsync().ConfigureAwait(false);
   
}
}


I keep a reference to IClientStreamWriter so that I can WriteAsync to it. After some time (potentially days, maybe more) the application might need to close the connection. Note that for now, I actually don’t event care about the response.

Is this valid use of client-side streaming ? Anything I should be careful about ?

Sam 

Jan Tattermusch

unread,
Jun 28, 2019, 4:57:07 AM6/28/19
to Sam, grpc.io
Keeping a client-streaming RPC open and sending messages (often times this would semantically be "notifications") to the server as they become available (=not immediately)  is one of the main use cases of client-side streaming and it's definitely a legitimate use case. 
Whenever using long-lived streaming RPC, you there are some additional concerns you need to be aware of. I highly recommend this talk https://www.youtube.com/watch?v=Naonb2XD_2Q

--
You received this message because you are subscribed to the Google Groups "grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email to grpc-io+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/grpc-io/613fea5a-fe51-40b5-a69f-cbf82190b797%40googlegroups.com.


--

Jan

Reply all
Reply to author
Forward
0 new messages