paullouisageneau / datachannel-wasm

C++ WebRTC Data Channels and WebSockets for WebAssembly in browsers
MIT License
148 stars 25 forks source link

ServiceWorker datachannel-wasm <=> window native datachannel #40

Closed guest271314 closed 2 years ago

guest271314 commented 2 years ago

Can this library create a WASM datachannel in a ServiceWorker and communicate with window?

In the source code window is referenced. ServiceWorker does not have a window. globalThis can be substituted for window below if in fact datachannel-wasm can be created in a ServiceWorker (or Worker).

https://github.com/paullouisageneau/datachannel-wasm/blob/bb1da1b7b1f9d4555aaf097d606bcad20eed7245/wasm/js/webrtc.js#L152

paullouisageneau commented 2 years ago

The WebRTC API is not exposed to Service Workers in browsers so sadly datachannel-wasm won't work in a Service Worker.

guest271314 commented 2 years ago

Ah, I was thinking you defined WebRTC in WASM, thus exposed WebRTC in any context. This https://rustwasm.github.io/wasm-bindgen/examples/webrtc_datachannel.html must not do that either.

paullouisageneau commented 2 years ago

Ah, I was thinking you defined WebRTC in WASM, thus exposed WebRTC in any context. This https://rustwasm.github.io/wasm-bindgen/examples/webrtc_datachannel.html must not do that either.

In browser contexts you can't implement a custom WebRTC stack as there are no lower-level network API, so you actually need to wrap the existing JS API. That's indeed what Rust bindings also do.