grpc / grpc-dart

The Dart language implementation of gRPC.
https://pub.dev/packages/grpc
Apache License 2.0
860 stars 271 forks source link

How to know when the server completed sending data to server stream channel on client side #596

Closed kienvtqhi closed 1 year ago

kienvtqhi commented 1 year ago

grpc-dart: 3.1.0 Hello,

I am using grpc server stream in my Flutter app to receive data from server side. How can the client side (Flutter app) know that when the server completed sending data to the stream? I used postman to test the server stream grpc function and it know it.

Screenshot 2022-12-22 at 17 41 56

Thank you so much

mraleph commented 1 year ago

I would expect that the stream is closed on the client side when server side closes the stream. Have you tried adding on onDone callback when listening for events from the stream?

kienvtqhi commented 1 year ago

@mraleph Thanks for your answer and sorry for the late response

Stream<Match> registerListenerForNewMatch(String userId) async* {
    try {
      await unregisterListenerMatchStream();
      CreateMatchStreamRequest channel = CreateMatchStreamRequest()..userId = userId;
      _chatMatchStream = grpcServiceClient!.createMatchStream(channel);

      if (_chatMatchStream != null) {
        logger.e(
            "gRPC match streaming...: ${DateTime.now().millisecondsSinceEpoch} -> $_chatMatchStream");
      }
      await for (var msg in _chatMatchStream!) {
        logger.e("gRPC match stream message received: $msg");
        yield msg;
      }
    } catch (e) {
      logger.e(
          "gRPC match subscripe error: ${DateTime.now().millisecondsSinceEpoch} -> $e");
      await unregisterListenerMatchStream();
    }
  }

Have you tried adding on onDone callback when listening for events from the stream?

I attached the code I am using to listen the event from the stream from gRPC server. How can I add onDone callback?

mraleph commented 1 year ago

In this case you don't need onDone. await for should terminate when the server stops sending data and closes the stream. Does not it not terminate for you?