grpc / grpc-dart

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

Support gRPC-Web protocol #43

Closed jakobr-google closed 5 years ago

jakobr-google commented 6 years ago

See the gRPC-Web protocol document for details.

We should be able to support this by making a GrpcWebChannel that implements the ClientChannel interface.

facundomedica commented 5 years ago

@joelmarks-wf you are seeing the releases on Github (that seems to be out of use). Check https://pub.dev/packages/grpc

joelmarks-wf commented 5 years ago

Ahhh. Great! Does that mean you can remove the last part from https://pub.dev/packages/grpc#notes "It currently supports the the Flutter and Dart native platforms. The potential addition of gRPC-Web is tracked in issue 43."?

mit-mit commented 5 years ago

We still need to do a bit more testing before we declare this fully done, but yes there should be enough here for testing. Do let us know if you try it, and how that worked!

ligustah commented 5 years ago

I tried it for one of my pet projects and it mostly worked like a charm, so thanks for that!

One of the issues is that it doesn't seem to work with more recent versions of the toolchain around webdev. Using these version:

webdev: 2.2.0
build_runner: 1.6.0
build_web_compilers: 2.1.1

Dart VM version: 2.3.2 (Tue Jun 11 15:29:47 2019 +0200) on "macos_x64"

I get this message when I run webdev serve:

[WARNING]build_web_compilers:entrypoint on web/main.dart: Skipping compiling tap|web/main.dart with ddc because some of its
transitive libraries have sdk dependencies that not supported on this platform:

grpc|lib/src/client/transport/http2_credentials.dart
grpc|lib/src/shared/security.dart
googleapis_auth|lib/src/oauth2_flows/auth_code.dart

https://github.com/dart-lang/build/blob/master/docs/faq.md#how-can-i-resolve-skipped-compiling-warnings

and my app doesn't run. Haven't tried building with dart2js, though. I moved to these version to make it work:

webdev: 1.0.0
build_runner: 1.3.3
build_web_compilers: 1.2.2

Dart VM version: 2.3.2 (Tue Jun 11 15:29:47 2019 +0200) on "macos_x64"

With these versions there are no warnings and the app works just fine in the browser.

The only other issue I ran into was with a bidirectional stream endpoint I had in my files. It sort of worked for one message, but then behaved really weird and often times would just disconnect. From what I understand it's not currently supported by the grpc-web spec. Also I used the improbable implementation embedded in my go application, which also explicitly states that this is not supported.

Maybe it would be possible to directly throw an exception in case someone tries to use such an endpoint over a grpc-web channel.

Other than that it worked great and simplified my code base a lot. Awesome work!

sigurdm commented 5 years ago

Have you tried v. 2.0.0+1 it should fix the issue you see with webdev?

sigurdm commented 5 years ago

Maybe it would be possible to directly throw an exception in case someone tries to use such an endpoint over a grpc-web channel.

Good point. I filed https://github.com/grpc/grpc-dart/issues/200

ligustah commented 5 years ago

Oh, hadn't seen the +1 release. I'll give it a shot later. Thanks a lot @sigurdm

ligustah commented 5 years ago

Have you tried v. 2.0.0+1 it should fix the issue you see with webdev?

Worked!

ghost commented 5 years ago

Is would be useful for everyone is there was an example that use GRPC-Web for FLutter Web, Desktop and Mobile.

sigurdm commented 5 years ago

Is would be useful for everyone is there was an example that use GRPC-Web for FLutter Web, Desktop and Mobile.

In the current form you cannot use grpc-web from flutter Desktop.

I'm not sure we have the resources to maintain another example currently. But would welcome external contributions.

ghost commented 5 years ago

Fair enough. I can make a demo repo. Can you explain why Flutter Desktop cannot use GRPC ? Is it related to AOT ?

On Wed, 26 Jun 2019, 15:47 Sigurd Meldgaard, notifications@github.com wrote:

Is would be useful for everyone is there was an example that use GRPC-Web for FLutter Web, Desktop and Mobile.

In the current form you cannot use grpc-web from flutter Desktop.

I'm not sure we have the resources to maintain another example currently. But would welcome external contributions.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/grpc/grpc-dart/issues/43?email_source=notifications&email_token=AE5YFQSTS7E5LUDYACROYPDP4NXOVA5CNFSM4EJBGAPKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODYTSOZI#issuecomment-505882469, or mute the thread https://github.com/notifications/unsubscribe-auth/AE5YFQTMYAUFNN6GWH546ZDP4NXOVANCNFSM4EJBGAPA .

sigurdm commented 5 years ago

It can use grpc, but not grpc-web. Grpc-web makes http requests using dart:html. Grpc makes http2 requests using dart:io sockets.

ghost commented 5 years ago

Ok makes sense. Thanks for the clarification. So the trick will be to use grpc for flutter mobile and desktop and grpc-web for flutter web.

I will work up a demo and post as a new issue

michaellee8 commented 5 years ago

Is there any way to use grpc-web protocol in Flutter mobile apps? My use case is a forum application that is very likely to be attacked using DDOS or other vectors, so I would have to use Cloudfare to protect my endpoints, but I still want to leverage the goodness in gRPC. However, Cloudfare does not support the HTTP/2 protocol that normal gRPC uses.

sigurdm commented 5 years ago

Currently grpc web is tied to HttpRequest from dart:html. It should be possible to generalize it. I opened an issue https://github.com/grpc/grpc-dart/issues/216.