grpc / grpc-dart

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

Failed to ping through web but OK on iOS #676

Closed profcoconut closed 8 months ago

profcoconut commented 8 months ago
Web: I am encountering a gRPC error when trying to connect from my Flutter application to a gRPC server. The error code is 14 with codeName UNAVAILABLE. iOS: It works. version: grpc: ^3.2.4 ## Repro steps Bascially it's a ping/pong service. Expected result: Works on both platform. Actual result: Web: ``` Connecting to VM Service at ws://127.0.0.1:60631/tnOg9GLHEZY=/ws to ping fetch seed 3 Caught error: gRPC Error (code: 14, codeName: UNAVAILABLE, message: Error connecting: Unsupported operation: Socket constructor, details: null, rawResponse: null, trailers: {}) ``` iOS: ``` flutter: to ping flutter: fetch seed flutter: Received response: Pong flutter: Received response: Application finished. ``` deps: ``` dependencies: flutter: sdk: flutter protobuf: ^3.1.0 provider: ^6.0.5 uuid: ^4.1.0 intl: ^0.18.1 font_awesome_flutter: ^10.6.0 url_launcher: ^6.1.14 webview_flutter: ^4.4.1 shared_preferences: ^2.2.2 grpc: ^3.2.4 ```
profcoconut commented 8 months ago

This is a CORS issue. This enovy config works for me:


# Admin settings
admin:
  access_log_path: /tmp/admin_access.log
  address:
    socket_address: 
      address: 0.0.0.0
      port_value: 9901

# Static resource configurations
static_resources:
  listeners:
    - name: listener_0
      address:
        socket_address: 
          address: 0.0.0.0
          port_value: 8080
      filter_chains:
        - filters:
            - name: envoy.filters.network.http_connection_manager
              typed_config:
                "@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager
                codec_type: auto
                stat_prefix: ingress_http
                route_config:
                  name: local_route
                  virtual_hosts:
                    - name: local_service
                      domains: ["*"]
                      cors:
                        allow_origin_string_match:
                          - safe_regex:
                              google_re2: {}
                              regex: '^https?://localhost:[0-9]{4,5}$'
                        allow_headers: "keep-alive,user-agent,cache-control,content-type,content-transfer-encoding,custom-header-1,x-accept-content-transfer-encoding,x-accept-response-streaming,x-user-agent,x-grpc-web,grpc-timeout"
                        max_age: "1d"
                      routes:
                        - match: 
                            prefix: "/"
                          route: 
                            cluster: sample_cluster
                            timeout: 
                              seconds: 60
                # HTTP filter settings
                http_filters:
                  # gRPC-Web filter
                  - name: envoy.filters.http.grpc_web
                    typed_config:
                      "@type": type.googleapis.com/envoy.extensions.filters.http.grpc_web.v3.GrpcWeb
                  # CORS filter
                  - name: envoy.filters.http.cors
                    typed_config:
                      "@type": type.googleapis.com/envoy.extensions.filters.http.cors.v3.Cors
                  # HTTP router filter
                  - name: envoy.filters.http.router
                    typed_config:
                      "@type": type.googleapis.com/envoy.extensions.filters.http.router.v3.Router

  # Cluster settings
  clusters:
    - name: sample_cluster
      connect_timeout: 0.25s
      type: logical_dns
      http2_protocol_options: {}
      load_assignment:
        cluster_name: sample_cluster
        endpoints:
          - lb_endpoints:
              - endpoint:
                  address:
                    socket_address:
                      address: localhost  # Change to your gRPC server address
                      port_value: 50051  # Change to your gRPC server port