grpc / grpc-web

gRPC for Web Clients
https://grpc.io
Apache License 2.0
8.46k stars 762 forks source link

Add support for custom transports #1277

Open notmedia opened 1 year ago

notmedia commented 1 year ago

Hi!

As we know Node.js doesn't support XMLHttpRequest by default. We can use polyfills and define them globally, for example this comment introduce solution

It works but Server Streaming doesn't work properly - we are receiving messages only when the stream is ended, more info here.

grpcweb allows server streaming methods to be called, but it doesn’t return data until the stream has closed.

I didn't test it in browsers, maybe this problem is only XHR polyfill related. I've tried all polyfills that I could find.

I've looked to @improbable-eng/grpc-web solution, there we can use custom transport and it already has Node.js HTTP transport. But seems to me this project was abandoned. I've talked with one contributor and he said that he doesn't have any connection to maintainers and the latest version was 10 months ago. He can't publish new versions to npm registry and I think this is a big problem.

So, here we have a great opportunity to make this lib a real standard of gRPC-Web. And it would be great if you provide ability to setup custom transport or at least Node.js HTTP.

For what needs I'm searching for this feature: First - electron apps which could use this library. Because of Node.js backend inside it's having problems. For example I'm working on desktop gRPC/gRPC-Web client and can't use this library, but really want :)

Second - integration tests written on top of Node.js also could make a good option for testing gRPC-Web requests.

sampajano commented 1 year ago

@notmedia Hi! Thanks so much for your interest and ideas here! 😃

I think is a good idea and indeed a potential area of improvement!

I wonder if you have any thoughts/preferences on the APIs here? (e.g. i haven't taken a close look at the solution by @improbable-eng/grpc-web, but wonder if something like what they had exactly fits your use case?)

Also, i'm wondering if you're open to contribute code here as well?

Thanks again! :)

notmedia commented 1 year ago

Hi! Thanks for the fast reply.

The @improbable-eng/grpc-web has Transport interface which anyone could implement, then pass it to gRPC client constructor and the client would use this transport.

If we will have this setup here, then we could implement http(s) request via system node.js http(s) module. Also when https is used we need to have ability to provide TLS credentials for Server-Side/Mutual TLS setup like I did here

I looked to the current implementation and seams to me it will take a lot of time to implement this feature. All methods use xhr without any abstraction. And I didn't work with Google Closure Compiler before. Right now I cloned the repo and trying to figure it out how it works.