Return response and stream together

113 views
Skip to first unread message

Srishti Kumari

unread,
Sep 13, 2022, 7:11:18 PM9/13/22
to grp...@googlegroups.com
Hi,
I wanted to know if its possible to stream events and also return a response from the same method in Grpc server call from the client.
how do we keep on streaming data and return a response too?

Thanks
Srishti Kumari

Easwar Swaminathan

unread,
Sep 14, 2022, 2:04:25 PM9/14/22
to grpc.io
Please provide more information like what language are you using, how does your rpc definition look like, what the server does, what the client does etc, and what you expect to see, but didnt. In the meantime, maybe this helps: https://grpc.io/docs/languages/go/basics/.

Srishti Kumari

unread,
Sep 14, 2022, 3:08:33 PM9/14/22
to Easwar Swaminathan, grpc.io
i am using C# here .My rpc looks like this:
rpc StreamEvents (TestUnitRequest) returns (stream Events);
 this rpc method is used to stream events and below rpc method is used to  subscribe to events ,load program api and log timestamp values and return a boolean value on successful load.
 rpc SendStatus(AppStatus) returns (CommandReply);

The client sends a request to load data and stream events from the Grpc server in the following way:
 var reply1 = client.SendStatus(new AppStatus {});
                    ExceptionMethod(reply1);
                   
                    var result = client.StreamEvents( new TestUnitRequest { });

I am trying to write the streamed events to file and return boolean value to the console as:
Console.WriteLine(reply1.IsAccepted);

                    Console.WriteLine("Streaming events.....");
                    StringBuilder sb6 = new StringBuilder();
                    foreach (var cmd in result.Events)
                    {
                        sb6.Append(cmd);
                        File.AppendAllText(filePath11 + "SampleTpEventlog.txt", sb6.ToString());
                    }
                    sb6.Clear();

But the streaming is returning null value.
Here is the server side code for rpc method sendStatus:
 disposable = service.SubscribeToEvents(OnEvents);
try
                    {
                        var val1 = await service.LoadTestProgram(appstatus.Dir, appstatus.Tpl, appstatus.Stpl, appstatus.Soc, appstatus.Xml, appstatus.Env, true, true);
                        // Console.WriteLine(now.ToString());
                      
                        retval = val1;
                       
                    }
                    catch (Exception e)
                    {

 errmsg = e.Message;
                     
                    }


 return new CommandReply
                    {
                       IsAccepted = retval
                     
                     
                    };

server side code for rpc method StreamEvents is:
 public override async Task StreamEvents(TestUnitRequest tur, IServerStreamWriter<Events> responseStream, ServerCallContext context)
        {
            await responseStream.WriteAsync(Events);

        }

Please let me know if this is enough.Server is on remote desktop and client on local desktop.

--
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/14610665-5324-47af-92ac-60e5d5bf53e1n%40googlegroups.com.

Easwar Swaminathan

unread,
Sep 14, 2022, 3:20:05 PM9/14/22
to grpc.io
Thanks Srishti. Someone with C# knowledge will take a look soon.

Jan Tattermusch

unread,
Oct 3, 2022, 3:31:43 PM10/3/22
to grpc.io

You cannot have a serverside handler that is both capable of sending a stream of responses AND sends a unary response at the same time - you have to choose one of the approaches. Since a handler that returns a stream of responses (using responseStream.WriteNextAsync() ) is the more general approach, you can use it, but you have to differentiate between the "normal" streaming responses and the "final" streaming response that concludes the stream. That is doable e.g. with having a protobuf message that can contain multiple different types of payload and then using the right kind of payload for each of the responses.

rpc StreamEvents (TestUnitRequest) returns (stream EventOrFinalResult);

(in this case the "EventOrFinalResult" message can contain either an "Event" or "FinalResult" message).
Reply all
Reply to author
Forward
0 new messages