I am building an app which will communicate with the other devices in my office network through WebRTC. For mobile phones in the network, I want to use a PWA for this job.
Can PWAs have a background service running which communicates with other devices over WebRTC?
For posterity, as of date WebRTC is not available in ServiceWorkers. See: Use WebRTC with WebWorker
It has been a request for a few years and may or may not be implemented in the future.
W3C Requests for WebRTC in ServiceWorker:
The current alternative it to use Transferable Streams. Of date Chrome supports Transferable streams.
https://github.com/whatwg/streams/blob/main/transferable-streams-explainer.md
Transferable Stream JS implementations
For browsers not supporting transferable streams you can use transferable ArrayBuffer.
https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Transferable_objects
You'll need to create the WebRTC channel in the window (non-worker) scope and transfer the Stream
or ArrayBuffer
to the ServiceWorker to complete the request.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With