proposals icon indicating copy to clipboard operation
proposals copied to clipboard

Is WebRTC usable?

Open Regenhardt opened this issue 4 years ago • 9 comments

I there a way to directly use WebRTC from WASM yet? P2P-Chat is a typical use case for web apps, and it would be nice to be able to use WebRTC APIs without js interop.

Unfortunately as someone only barely touching the topic, it's very complicated to find a direct answer to this online, as there are no examples.

Regenhardt avatar Jan 29 '21 10:01 Regenhardt

In the browser everything you can do from JavaScript can be done from WebAssembly, assuming you have some "glue" code working that bridges calls from Wasm to the JavaScript Web API. How readily and how easily the glue code is available depends on the source language that you compile to Wasm.

MaxDesiatov avatar Jan 29 '21 10:01 MaxDesiatov

Not sure what you mean by "directly" though. If that assumes a complete absence of any JavaScript glue code, then the answer is "no". Not only for WebRTC, but for any API. I believe that will be the case until implementations for proposals like Interface Types start becoming available in the actual browser.

MaxDesiatov avatar Jan 29 '21 10:01 MaxDesiatov

If I understand correctly, when you instantiate a wasm module with an API import, (assuming not additional JS glue code), most engines will 'import' a call to the actual API. That still has a JS-style internal interface (calling C from JS) and that is not likely to go away any time soon.

fgmccabe avatar Jan 29 '21 16:01 fgmccabe

Just curious, what API import are you referring to? Can you provide an example?

MaxDesiatov avatar Jan 29 '21 16:01 MaxDesiatov

Any WebAPI; especially one that only takes/provides numeric results. (String arguments/results do require glue code in one form or another.)

fgmccabe avatar Jan 29 '21 16:01 fgmccabe

Ah, right, you mean by providing every function as something to be imported in a Wasm module? As there's no such thing as a WebRTC module, I imagine you would have to enumerate every function manually and pass as separate imported functions, right? And then in WebRTC most of them aren't plainly numeric.

MaxDesiatov avatar Jan 29 '21 16:01 MaxDesiatov

That is the strength of it. With interface types, the glue code would be auto-generated and (depending on the specifics of the situation) in pure wasm. However, for WebAPIs, this also depends on browser implementations, ...

fgmccabe avatar Jan 29 '21 16:01 fgmccabe

Looks like i still have a big gap in my understanding of WASM.

Can a WASM application interact with the browser in any way without using javascript? Like making http requests? Or is the access to the browser always done through javascript interop? I thought the plan was to make something that could actually replace JS in the long run.

If literally every interaction between WASM and the browser is through JS anyway, would it be just as fast to explicitely use JS interop from my application, or would the "WASM-native" glue code have optimizations or ways to work faster?

Regenhardt avatar Feb 01 '21 08:02 Regenhardt

@Regenhardt

Can a WASM application interact with the browser in any way without using javascript? Like making http requests?

No. You can't even load wasm without JS (e.g. you don't have

Or is the access to the browser always done through javascript interop? The access to the DOM is done through JS only.

I thought the plan was to make something that could actually replace JS in the long run.

I think the plan/roadmap was to ship something useful asap so that people can use it and provide feedback and add more features/perf optimisations (i.e. GC, direct DOM manipulation ) later.

If literally every interaction between WASM and the browser is through JS anyway, would it be just as fast to explicitely use JS interop from my application, or would the "WASM-native" glue code have optimizations or ways to work faster?

  • While the wasm code can be faster and more efficient than JS(computation wise), overall the performance of the app may be worse than the performance of a pure JS app if you have many DOM operations (due the overhead between the browser, js engine and WASM).

  • Chances are that in a WebRTC app the performance will be worse if it's powered by wasm as most of the operations will be DOM operations anyway.

  • When WASM will get native DOM access you can remove the JS glue and your app will perform at the highest performance available in the browser.

  • Here [0] is a WebRTC library in Go which compiles to WASM as well. Even the performance may suffer due the JS interop you still have the advantage to use the same language/library/code on the server, client (i.e. mobile, desktop app) and in the browser.

[0] https://github.com/pion/webrtc

Disclaimer: I'm not part of the wasm-wg group. I work on a wasm based app compiled from Go.

mihaiav avatar Feb 15 '21 17:02 mihaiav

The question here seems answered; if there are any further questions, please file new isssues in the design repo.

sunfishcode avatar Mar 04 '24 17:03 sunfishcode