grpc / grpc-dart

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

Whether it supports flutter-web? #263

Closed yuhorun closed 4 years ago

yuhorun commented 4 years ago

I can use it normally in a dart function.When I integrate this function into the flutter-web,The request will give an error.Can anyone use grpc normally in flutter-web?I wonder if there is something wrong with my code. thank you!

dependencies: flutter: sdk: flutter grpc: ^2.1.3 protobuf: ^1.0.1 cupertino_icons: ^0.1.2

Repro steps

nomal: image

flutter-web Error: image

Expected result: The request should be invoked normally.and return a result. Actual result: when call $grpc.ResponseFuture<$0.ResponseHello> sayHello($0.RequestHello request,{$grpc.CallOptions options}) return error: Caught error: gRPC Error (14, Error connecting: Unsupported operation: Socket constructor)

wstrange commented 4 years ago

Flutter web runs in the browser and does not support dart.io. I think you want the package:grpc/grpc_web.dart package.

yuhorun commented 4 years ago

this is my code:

` import 'package:flutter/material.dart'; import 'dart:async'; import 'package:grpc/grpc.dart'; import 'package:application/model/hello.pbgrpc.dart';

void main() => runApp(new FlutterGrpcApp());

class FlutterGrpcApp extends StatefulWidget { _FlutterGrpcAppState createState() => _FlutterGrpcAppState(); }

class _FlutterGrpcAppState extends State {

@override Widget build(BuildContext context) { return new MaterialApp( title: 'Flutter Demo', theme: new ThemeData( primarySwatch: Colors.blue, ), home: Scaffold( appBar: AppBar( title: Text("Grpc ❤️ Flutter"), ), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center, children: [ FlatButton( onPressed: () async { await _sayHello();}, color: Theme.of(context).primaryColor, child: Text( "tap", style: TextStyle(color: Colors.white), )), ], ), ), ), ); }

Future _sayHello() async { final channel = ClientChannel('localhost', port: 3000, options: const ChannelOptions(credentials: ChannelCredentials.insecure())); final requestMSG = 'world'; final stub = HelloServiceClient(channel); try { final response = await stub.sayHello(RequestHello()..request = requestMSG); print('Greeter client received: ${response.response}'); } catch (e) { print('Caught error: $e'); } await channel.shutdown(); } }

`

yuhorun commented 4 years ago

Flutter web runs in the browser and does not support dart.io. I think you want the package:grpc/grpc_web.dart package.

Because flutterweb is converted to js.i shoud use js-grpc?right? Seems to have found a way thank you!

long1eu commented 4 years ago

@yuhorun can you share your solution? I'm using the grpc_web but I get a gRPC Error (14, XhrConnection status 0)

danielleiszen commented 4 years ago

@yuhorun share your solution please, I am stuck with the same problem. thx

fzyzcjy commented 3 years ago

How did you solve it? I have the same problem! Thank you

TENX-S commented 3 years ago

@yuhorun can you share your solution? I'm using the grpc_web but I get a gRPC Error (14, XhrConnection status 0)

I have the same issue, any update ?

yuhorun commented 3 years ago

@yuhorun can you share your solution? I'm using the grpc_web but I get a gRPC Error (14, XhrConnection status 0)

I have the same issue, any update ?

grpc/grpc_web.dart , you can try this package. any question can contact me.

yuhorun commented 3 years ago

How did you solve it? I have the same problem! Thank you

grpc/grpc_web.dart , you can try this package. any question can contact me.

yuhorun commented 3 years ago

How did you solve it? I have the same problem! Thank you

grpc/grpc_web.dart , you can try this package. any question can contact me.

TENX-S commented 3 years ago

How did you solve it? I have the same problem! Thank you

grpc/grpc_web.dart , you can try this package. any question can contact me.

Yes, but:

GrpcWebClientChannel channel = GrpcWebClientChannel.xhr(Uri.parse('http://$serverAddr:$serverPort'));

doesn't work. I got gRPC Error (14, XhrConnection status 0). And the server side works well with bloomRPC. I might need to use envoy as a web proxy in front of my server?

yuhorun commented 3 years ago

How did you solve it? I have the same problem! Thank you

grpc/grpc_web.dart , you can try this package. any question can contact me.

Yes, but:

GrpcWebClientChannel channel = GrpcWebClientChannel.xhr(Uri.parse('http://$serverAddr:$serverPort'));

doesn't work. I got gRPC Error (14, XhrConnection status 0). And the server side works well with bloomRPC. I might need to use envoy as a web proxy in front of my server?

sorry,time is long. i cant find my code.but i remember use no proxy。you can see this replied example: envoy. (although i use no proxy, but i success.)

emicklei commented 1 year ago

I am using a Go backend and used the github.com/improbable-eng/grpc-web/go/grpcweb package instead of a proxy such as envoy. The GrpcWebClientChannel is also working with localhost for development.