grpc-web icon indicating copy to clipboard operation
grpc-web copied to clipboard

Use fetch instead of goog.net.XhrIo/XMLHttpRequest

Open avm99963 opened this issue 3 years ago • 6 comments

I'm trying to use the gRPC-Web JS library inside a service worker, where XMLHttpRequest isn't exposed, so gRPC requests don't succeed (instead I get the error ReferenceError: XMLHttpRequest is not defined).

The reason why I want to use gRPC-Web inside a service worker is that I'm developing a Chrome extension, and need to call a gRPC method every 30 minutes, which can only happen inside a service worker.

It would thus be great if the library used fetch instead of goog.net.XhrIo/XMLHttpRequest.

avm99963 avatar Sep 06 '21 00:09 avm99963

Just found out that using something like https://github.com/apple502j/xhr-shim lets me work around this problem easily!

Update: xhr-shim doesn't work well (requests are sent but responses are not being received), but it seems like https://github.com/markis/sw-xhr does work.

avm99963 avatar Sep 06 '21 01:09 avm99963

Glad you found a solution! 😃

More native Service Worker support would definitely be a plus for gRPC-web too :)

sampajano avatar Sep 10 '21 20:09 sampajano

FYI -- an update about the reason we cannot yet switch to fetch is provided in this related thread :)

sampajano avatar Jan 27 '22 23:01 sampajano