connect-es icon indicating copy to clipboard operation
connect-es copied to clipboard

JSON support for gRPC-Web transport

Open akshayjshah opened this issue 3 years ago • 6 comments
trafficstars

Is your feature request related to a problem? Please describe.

Using the gRPC-Web transport locks me into using binary Protobuf serialization, which makes the network inspector fully useless.

Describe the solution you'd like

I'd like the gRPC-Web transport to support JSON serialization, like the Connect transport. This would make the network inspector only partially useless :)

Describe alternatives you've considered

I could switch to the Connect protocol, but this is a more invasive change. Why not support the same switch in both protocols?

Additional context

Originally brought up by @tannerlinsley in Slack.

akshayjshah avatar Sep 09 '22 22:09 akshayjshah

Regarding the point about the network inspector, it'd be possible to build a browser extension for that :-).

fubhy avatar Sep 19 '22 08:09 fubhy

Regarding the point about the network inspector, it'd be possible to build a browser extension for that :-).

Definitely possible, but difficult to do in a way that's universally usable. Converting binary protobuf to JSON requires the schema, and there's no standard for fetching a schema. (That's what Buf is building!) The gRPC reflection protocol is sort of close, but it requires bidirectional streaming....which isn't feasible from browsers.

In short, where would the browser extension get the schema from?

akshayjshah avatar Sep 20 '22 00:09 akshayjshah

https://github.com/bufbuild/protobuf-es claims to support the canonical json format, so I assume it's already being generated with access to the schema. This issue is just to wire up the option, is that right?

derekperkins avatar Sep 20 '22 01:09 derekperkins

https://github.com/bufbuild/protobuf-es claims to support the canonical json format, so I assume it's already being generated with access to the schema. This issue is just to wire up the option, is that right?

That's correct!

akshayjshah avatar Sep 20 '22 02:09 akshayjshah

There is a browser extension for the official gRPC-web client: grpc-web-devtools.

It works by adding an interceptor to the the client, and sending payloads to the extension, which renders it in a dedicated inspector tab.

There are a few downsides with this approach:

  • Users need to install the extension.
  • The web application needs to opt in, passing a client object to a function provided by the extension.
  • The extension will not show the actual network traffic, only what is visible in user land. That means you don't see headers hidden because of CORS, and have to correlate failed requests with requests in the network inspector.

For those reasons, I believe that using JSON on the wire provides a better UX than an extension, but I don't want to disregard use cases where the Connect protocol isn't an option. I think the best way forward here is to look into gRPC-web + JSON before we look into browser extensions.

timostamm avatar Sep 20 '22 07:09 timostamm

I highly agree with Timo here. As someone who as built custom dev tools and felt their immediate downsides, the idea of a browser extension as the only way to see my network traffic is scary. Pushing hard for a json over the wire implementation before anything else. On Sep 20, 2022 at 1:58 AM -0600, Timo Stamm @.***>, wrote:

There is a browser extension for the official gRPC-web client: grpc-web-devtools. It works by adding an interceptor to the the client, and sending payloads to the extension, which renders it in a dedicated inspector tab. There are a few downsides with this approach:

• Users need to install the extension. • The web application needs to opt in, passing a client object to a function provided by the extension. • The extension will not show the actual network traffic, only what is visible in user land. That means you don't see headers hidden because of CORS, and have to correlate failed requests with requests in the network inspector.

For those reasons, I believe that using JSON on the wire provides a better UX than an extension, but I don't want to disregard use cases where the Connect protocol isn't an option. I think the best way forward here is to look into gRPC-web + JSON before we look into browser extensions. — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you were mentioned.Message ID: @.***>

tannerlinsley avatar Sep 20 '22 13:09 tannerlinsley

Just a quick update: We've added support for the JSON format to the gRPC-web transport a while back. It will be released in the first version that ships with the new shared package @bufbuild/connect-core, as described in the road map here.

timostamm avatar Dec 08 '22 12:12 timostamm

Just checkin in on the progress here as v1 seems to have landed. Is this possible in the latest release? We are also investigating switching our backend to use the connect protocol if this isn't ready yet.

tannerlinsley avatar Feb 15 '23 20:02 tannerlinsley

The connect-web package that uses connect-core is not yet published. Only connect-node is using this right now. We hope to have connect-web updated within the next couple weeks.

smaye81 avatar Feb 15 '23 23:02 smaye81

Support for JSON in the gRPC-web transport is finally available with v0.8.1 and can be enabled with a boolean option:

const transport = createGrpcWebTransport({
  baseUrl: "https://demo.connect.build",
+ useBinaryFormat: false,
});

timostamm avatar Feb 25 '23 11:02 timostamm