grpc / grpc-dart

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

[help needed] Use generated service methods to process gRPC-web data. #685

Closed geoai777 closed 11 months ago

geoai777 commented 11 months ago

short description

Building client-server app, here's diagram:

          backend             transport        various clients

[             gRPC(Server) ] -- gRPC -- [ flutter app + bloc + gRPC ] 
[ gRPC + Postgres backend  ]
[    gRPC-web(HttpServer?) ] -- gRPCw -- [ flutter web + bloc + gRPC-web ]

Everything works on gRPC side, gRPC web gets data from flutter web application. Is it possible to make gRPC methods to process incoming gRPC data or what will be best solution to process that data?

versions

dart 3.2.3

  grpc: ^3.2.4
  protobuf: ^3.1.0

current code

  final server = Server.create(
    services: [
      SiteMethodsService(databaseCore),
      CategoryMethodsService(databaseCore)
    ],
    interceptors: [],
    codecRegistry: CodecRegistry(codecs: const [GzipCodec(), IdentityCodec()]),
  );

  print('Server:');
  /// -[ gRPC ]--
  await server.serve(port: rpcPort, shared: true);
  print(' gRPC port: ${server.port}');
  /// -------------------------- up until this line everything works fine

  /// -[ web ]--
  final webServer = await HttpServer.bind(
    '0.0.0.0',
    rpcPort,
    shared: true,
  );
  print(' gRPC-web port ${webServer.port}');
  /// ---- this also works, but has no methods to process data, what is native solution to direct this data to methods?

  await for (HttpRequest request in webServer) {

    print(HttpConnectionInfo);
    print(request.contentLength);
    print(request.response.toString());
  }

Details

AFAICT in gRPC server.serve all comes to ServerHandler.handle() method at pub.dev\grpc-3.2.4\lib\src\server\handler.dart. At first glance looks like same method can be used to parse gRPC web part.

Advise needed.

mosuem commented 11 months ago

If I understand correctly, you want to process gRPC Web requests in your server. There is currently no implementation for this, see #216. You could use a proxy, see https://grpc.io/docs/platforms/web/basics/.

geoai777 commented 10 months ago

I'd rather write it :) Proxy is good, but a bit excessive in my opinion.