Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WASM: support DialOptions.HTTPClient #373

Open
d-z-m opened this issue Feb 28, 2023 · 16 comments
Open

WASM: support DialOptions.HTTPClient #373

d-z-m opened this issue Feb 28, 2023 · 16 comments
Milestone

Comments

@d-z-m
Copy link

d-z-m commented Feb 28, 2023

Support configuring a custom http.Client for use in a Wasm deployment.

This would support making websocket connections through something like gvisor netstack.

An example of such an http.Client is here.

@nhooyr
Copy link
Contributor

nhooyr commented Feb 28, 2023

For Wasm deployment, you cannot pass in a custom http.Client because we use the JS WebSocket API directly and it doesn't let you tunnel websocket connections through things like that. You'd have to wrap the WebSocket itself rather than wrap the WebSocket in something or use a reverse proxy.

@nhooyr nhooyr closed this as completed Feb 28, 2023
@d-z-m
Copy link
Author

d-z-m commented Feb 28, 2023

For Wasm deployment, you cannot pass in a custom http.Client because we use the JS WebSocket API directly and it doesn't let you tunnel websocket connections through things like that.

I had thought you would simply use the http.Client's dial functions to make the websocket connection. Is that not the case?

@d-z-m
Copy link
Author

d-z-m commented Feb 28, 2023

In this case, I don't need the browser API at all, and would like it to behave just like it would in dial.go .

@nhooyr
Copy link
Contributor

nhooyr commented Feb 28, 2023

I had thought you would simply use the http.Client's dial functions to make the websocket connection. Is that not the case?

I can't make it any clearer.
Give this file a read https://github.com/nhooyr/websocket/blob/master/ws_js.go to understand what's going on.

@d-z-m
Copy link
Author

d-z-m commented Mar 1, 2023

I understand that to make this library work in a Wasm environment, you hook into the JS WebSocket API directly.

The issue I'm dealing with is: I would like to use the library to make WebSocket connections over a virtual network connection. E.g. an http.Client with a custom http.Transport.

I understand that the golang Wasm http.DefaultClient uses the Fetch API under the hood. However, http.Client still supports custom http.Transports when compiling to Wasm.

Ideally, this library would just sit on top of that, and negotiate the websocket in the normal way it would when compiling for linux x86 (i.e. invoking the http.Client's Dial and DialContext, etc.)

@nhooyr
Copy link
Contributor

nhooyr commented Mar 1, 2023

Fair enough, let me look into it. It's been a few years since I wrote the Wasm code.

@nhooyr nhooyr reopened this Mar 1, 2023
@nhooyr
Copy link
Contributor

nhooyr commented Mar 6, 2023

Ah ok I understand your request now. I'll change things such that *http.Client is an accepted field but ignored with clear documentation if it doesn't have a custom transport.

@nhooyr
Copy link
Contributor

nhooyr commented Mar 6, 2023

Or hold on actually so you're compiling not for browsers but for javascript in general. I'm not sure if I should support such a usecase. Then I need to add Accept support too.

@d-z-m
Copy link
Author

d-z-m commented Mar 9, 2023

Ah ok I understand your request now. I'll change things such that *http.Client is an accepted field but ignored with clear documentation if it doesn't have a custom transport.

Nice, this is what I was thinking.

Or hold on actually so you're compiling not for browsers but for javascript in general.

I'm not sure exactly what you mean by this, can you elaborate? I am compiling for browsers, i.e. GOOS=js GOARCH=wasm

@nhooyr
Copy link
Contributor

nhooyr commented Mar 9, 2023

I'm not sure exactly what you mean by this, can you elaborate? I am compiling for browsers, i.e. GOOS=js GOARCH=wasm

How could you have a custom transport in a browser? Unless it was through another WebSocket? Or I guess maybe over WebRTC?

@d-z-m
Copy link
Author

d-z-m commented Mar 11, 2023

How could you have a custom transport in a browser? Unless it was through another WebSocket? Or I guess maybe over WebRTC?

You got it, it would be another WebSocket. I.e. your library, but in two different contexts.

Imagine something like Tailscale's web-based ssh client, but instead of ssh, its a WebSocket client.

@nhooyr
Copy link
Contributor

nhooyr commented Mar 11, 2023

Alright sounds good, that's a fair use case I should support.

@Hoodie-Kang
Copy link

Hoodie-Kang commented Apr 12, 2023

@nhooyr So is this in progress?
I really hope it supports WASM

@nhooyr
Copy link
Contributor

nhooyr commented Sep 28, 2023

In the release after the next yes.

@flimzy
Copy link

flimzy commented Aug 18, 2024

I would like to second this request, and expand it to use identical DialOptions structs for WASM and non-WASM, with the understanding that certain options are ignored in WASM.

Having different APIs for different build targets makes it very difficult to support multiple platforms. It puts an extra burden on every third-party library that uses this one, regardless of whether or not the third-party library author cares about WASM support.

In particular, https://github.com/stackrox/go-grpc-http1 does not have any special handling for WASM build targets, so when using that library, it fails to compile for WASM.

@flimzy
Copy link

flimzy commented Aug 18, 2024

I'd also like to point out that WASM does not imply that the code is running in the browser.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants