tinygo-org / tinygo

Go compiler for small places. Microcontrollers, WebAssembly (WASM/WASI), and command-line tools. Based on LLVM.
https://tinygo.org
Other
14.71k stars 858 forks source link

Can wasm use httpclient? #850

Open cyanBone opened 4 years ago

cyanBone commented 4 years ago

Can wasm use httpclient?

itstmyi commented 4 years ago

FYR : https://github.com/vugu/vugu One example : https://github.com/vugu/vugu/tree/master/examples/fetch-and-display

Good luck .

xaionaro commented 4 years ago

@itstmyi :

bradleypeabody commented 4 years ago

I'm considering making a subset of the net/http package intended for client use in Vugu. The idea would be to make things like http.Get work the same way they do with Go's wasm implementation of that package.

Most of the things that don't compile in net/http are not useful under wasm anyway (in the browser I mean) - you cannot create raw sockets to do the various low-level things that are failing to compile. (i.e. I'm guessing that simply omitting via build tag whatever code is calling that asn1 stuff above would probably be an effective solution for that error for wasm, at least for now.). I do understand that the situation is different on microcontrollers, where it would be really cool to have a working http client and server for environments where it's possible.

When I put this alternate http package together for Vugu I'll also create an issue here to discuss if there would be any use in having this as part of TinyGo's stdlib. (A similar question exists for vjson, which is a subset of encoding/json that uses type switches and so does not require reflect.New, etc.)

bgould commented 4 years ago

@bradleypeabody what you are saying is very doable... we have this for microcontrollers which was intended to be a bridge towards having a workable net package on platforms with no OS: https://github.com/tinygo-org/drivers/tree/master/net - currently it allows for having a unified API whether you are using the espat or wifinina packages, though I think it could definitely use some work. I'm not sure if that would be useful for wasm but just pointing it out because it sort of proves out a concept.

As far as http goes, I've been able to modify and use https://github.com/gorilla/http on top of the drivers/net package and it works well enough, though it is not exactly a net/http-like API. I haven't looked at it in a bit so I'm not sure what shape its in but I could try to get it ready to share soon. I also had this all working in combination with https://github.com/valyala/fastjson (but it was kind of flaky, might work better in wasm).

All this is to say that I think you idea is very feasible and I look forward to seeing the result.

aykevl commented 4 years ago

I think that could work in the short term. However, in the longer term I want to make it possible to compile the standard library net/http package. I believe it has some support for running under WebAssembly in a browser.

bradleypeabody commented 4 years ago

Cool, when I dive in on this I will look at that possibility too - perhaps just a few small build tag changes would allow net/http to compile (although it would require forking that package, but it might be worth it and if the changes are simple it should be easy to maintain, possibly even scriptable).