Skip to main content

# Server-streaming

The client must initiate the connection. Is : one call -> several responsse

Server streaming RPCs where the client sends a request to the server and gets a stream to read a sequence of messages back. The client reads from the returned stream until there are no more messages. gRPC guarantees message ordering within an individual RPC call.

    rpc LotsOfReplies(HelloRequest) returns (stream HelloResponse);

tip

is not the same as using SignalR, it uses a single TCP Connection

Title

            public override async Task ServerStream(Request request, IServerStreamWriter<Response> responseStream, ServerCallContext context)
            {
                for (int i = 0; i < 10000; i++)
                {
                    var message = new Response
                    {
                        Message = "Hello " + i
                    };

                    await responseStream.WriteAsync(message);
                }
                #region responseHeaders
                Metadata.Entry myHeader = new Metadata.Entry("my-fake-header", "grpc-header");
                #endregion
                context.ResponseTrailers.Add(myHeader);
            }

Title

                var cts = new CancellationTokenSource();
                using var streamingCall = client.ServerStream(new Request(), cancellationToken: cts.Token);

                //var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5));
                //using var streamingCall = client.ServerStream(new Server.Request(),
                //                                            deadline: DateTime.UtcNow.AddMilliseconds(1),
                //                                            cancellationToken: cts.Token);

                try
                {
                    await foreach (Response response in streamingCall.ResponseStream.ReadAllAsync(cancellationToken: cts.Token))
                    {
                        Console.WriteLine($"{response.Message}");
                    }

                    var trailers = streamingCall.GetTrailers();
                    var myValue = trailers.GetValue("my-fake-header");
                    Console.WriteLine($"found some trailer values in the gRPC response:{myValue}");

                }
                catch (RpcException ex) when (ex.StatusCode == StatusCode.DeadlineExceeded)
                {
                    Console.WriteLine("Your greetings timeout'd :)).");
                }