How to do Asynchronous streaming with gRPC?

1,593

Solution 1

The scope of your question is too large. You have to make decisions about what you're going to stream, are you trying to stream one image frame? Keep in mind that gRPC advocates to either redesign your system or use something else if you're expecting to send more then 1MB per message.

I'm not familiar with dart but assuming you need to stream the images to the client running dart you can have the client send a request to the python server and then have the python server stream the frames back. The proto could look like this:

service FrameService {

  rpc FrameStreamer(FrameRequest) returns (stream Frame) {}

}

message FrameRequest {
    //empty
}

message Frame {
    //put your data in here, perhaps bytes?
    //or an frame id, etc.
}

Solution 2

This is quite a big topic, but you asked for a dart-client so I assume you have a ".proto"-file which looks like this and a working python-server.

If you want to connect you dart-client to an asynchronous stream you have to do somithing like this.

  typedef FrameStreameCallback = Function(Frame);

  ResponseStream<Frame> _frameStream;
  void getFrames(FrameStreameCallback cb) {
    FrameRequest req = new FrameRequest();
    //Set data of req and so on...

    _frameStream =  _stub.FrameStreamer(req);

    _frameStream.listen((Frame newFrame) {
      //Send callback when a new Frame is received
      cb(newFrame);
    });
    _messageStream.handleError((onError) {
      print("An error occured in frame stream " + onError);
    });
  }

Then you call this method like that:

obj.getFrames((Frame f){
  print("New Frame received" + f.toString());
});
Share:
1,593
satz
Author by

satz

Updated on December 09, 2022

Comments

  • satz
    satz over 1 year

    I am trying to do real-time image analysis with dart(flutter) client and python server with gRPC api. How to setup asynchronous image streaming with dart-client in gRPC?(like websockets)