Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Questions about gRPC support in web browsers and how they utilize HTTP2

Upon discovering gRPC, I stumbled across this blog post

Why isn’t everyone already using gRPC in their SPAs?

Traditionally it’s not been possible to use gRPC from browser-based applications, because gRPC requires HTTP/2, and browsers don’t expose any APIs that let JS/WASM code control HTTP/2 requests directly.

But there is a solution! gRPC-Web is an extension to gRPC which makes it compatible with browser-based code (technically, it’s a way of doing gRPC over HTTP/1.1 requests). gRPC-Web hasn’t become prevalent yet because not many server or client frameworks have offered support for it… until now.

ASP.NET Core has offered great gRPC support since the 3.0 release. And now, building on this, we’re about to ship preview support for gRPC-Web on both server and client. If you want to dig into the details, here’s the excellent pull request from James Newton-King where it’s all implemented.

There is some good information here, but the post is around a year old at this point.

There are also some major pushes from Microsoft with .NET and Blazor technology recently.

It looks like grpc-web is pretty well maintained and always adding a lot of language support, so that's something to keep an eye on... but as I understand it grpc-web is still built to operate over HTTP1.1?

For me, another question that still remains why HTTP2 is not supported through browser APIs, to which I cannot find any documentation on.

I would love to start using gRPC, but am also concerned about the cons that might come with it.

Thank you for any explanations to my lack of understanding.

Note there is a slightly related question on SO about this here, to which the answers were not totally comprehensive and older.

like image 245
spencer741 Avatar asked Jan 21 '21 08:01

spencer741


People also ask

Does gRPC work with browsers?

gRPC-Web. gRPC-Web allows browser apps to call gRPC services with the gRPC-Web client and Protobuf. Similar to normal gRPC, but it has a slightly different wire-protocol, which makes it compatible with HTTP/1.1 and browsers. Requires the browser app to generate a gRPC client from a .

Does gRPC require HTTP2?

gRPC heavily uses HTTP/2 features and no browser provides the level of control required over web requests to support a gRPC client. For example, browsers do not allow a caller to require that HTTP/2 be used, or provide access to underlying HTTP/2 frames.

Can we use gRPC in frontend?

Using gRPC-web in the frontend is pretty simple, as shown by the example below. const client = new TimeServiceClient("http://localhost:8080", null, null); // This is a neat chrome extension that allows you to spy on grpc-web traffic just like you would on normal traffic.

Why is gRPC-Web needed?

The web client of gRPC-Web allows for easier coordination between the browser and back end services of your application, and continues to provide compatibility wherever you implement the protocol. It also removes the need to configure separate HTTP clients between backend services and your application.

What is gRPC-web and how do I use it?

gRPC on ASP.NET Core offers two browser-compatible solutions: gRPC-Web allows browser apps to call gRPC services with the gRPC-Web client and Protobuf. gRPC-Web requires the browser app to generate a gRPC client. gRPC-Web allows browser apps to benefit from the high-performance and low network usage of gRPC. .NET has built-in support for gRPC-Web.

Is it possible to call a gRPC service from a browser?

ASP. NET Core It's not possible to directly call a gRPC service from a browser. gRPC uses HTTP/2 features, and no browser provides the level of control required over web requests to support a gRPC client. gRPC on ASP.NET Core offers two browser-compatible solutions:

How do I translate between gRPC-web and HTTP requests?

A mandatory proxy for translating between gRPC-Web requests and gRPC HTTP/2 responses. The basic idea is to have the browser send normal HTTP requests (with Fetch or XHR) and have a small proxy in front of the gRPC server to translate the requests and responses to something the browser can use.

Why choose gRPC over HTTP/2?

They are well served by choosing gRPC, whether it be for resiliency, performance, long-lived or short-lived communication, customizability, or simply knowing that their protocol will scale to extraordinarily massive traffic while remaining efficient all the way. To get going with gRPC and HTTP/2 right away, check out gRPC’s Getting Started guides.


1 Answers

I have used grpc in my projects and understand your questions about it. The first two questions can be answered via a quote from grpc.io followed by some elaboration.

- For me, another question that still remains why HTTP2 is not supported through browser APIs, to which I cannot find any documentation on.

- It looks like grpc-web is pretty well maintained and always adding a lot of language support, so that's something to keep an eye on... but as I understand it grpc-web is still built to operate over HTTP1.1?

It is currently impossible to implement the HTTP/2 gRPC spec3 in the browser, as there is simply no browser API with enough fine-grained control over the requests. For example: there is no way to force the use of HTTP/2, and even if there was, raw HTTP/2 frames are inaccessible in browsers. The gRPC-Web spec starts from the point of view of the HTTP/2 spec, and then defines the differences. quote reference

- I would love to start using gRPC, but am also concerned about the
cons that might come with it.

I puplished an story about gRpc. You should read. This can be helpful to understand gRPC.

I also want to talk about this topic. Why do you want to use gRPC? Is this about the speed of Http2 and gRPC? Is Http1.1 is old technology? Today, REST protocols work on Http1.1. If they start to use Http2.0, there won't be any changes to these interfaces. Also, REST is faster than gRPC if you don't work with streaming. gRPC has a better advantage when it comes to speed.

Below, I linked the supported types of RPC from GRPC-WEB

enter image description here

like image 55
Eyup Can ARSLAN Avatar answered Oct 20 '22 00:10

Eyup Can ARSLAN