Skip to main content

# Client streaming

The Client must initiate the connection, but it can send several calls over a single TCP connection Is : several calls -> one response

Client streaming RPCs where the client writes a sequence of messages and sends them to the server, again using a provided stream. Once the client has finished writing the messages, it waits for the server to read them and return its response. Again gRPC guarantees message ordering within an individual RPC call.

cam

    rpc LotsOfGreetings(stream HelloRequest) returns (HelloResponse);

            public override async Task<Response> ClientStream(IAsyncStreamReader<Request> requestStream, ServerCallContext context)
            {
                var baseMessage = "I got ";
                Response reply = new Response() { Message = baseMessage };

                while (await requestStream.MoveNext())
                {

                    var payload = requestStream.Current;
                    Console.WriteLine($"I got a request with: {payload}");
                    reply.Message = baseMessage + payload.ContentValue.ToString();
                }
                return reply;
            }

Title

    try
    {
        using var call = client.ClientStream();

        for (var i = 0; i < 10000; i++)
        {

            await call.RequestStream.WriteAsync(new Request { ContentValue = i.ToString() });
        }

        await call.RequestStream.CompleteAsync();
        Response response = await call;

        Console.WriteLine($"{response.Message} is the last value server received");
    }
    catch (RpcException ex) when (ex.StatusCode == StatusCode.Cancelled)
    {
        Console.WriteLine("Stream cancelled.");
    }