Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a PWA run a WebRTC connection in the background?

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?

like image 342
darkhorse Avatar asked Oct 16 '25 03:10

darkhorse


1 Answers

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:

  • https://github.com/w3c/webrtc-pc/issues/230
  • https://github.com/w3c/ServiceWorker/issues/1522

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

  • https://github.com/MattiasBuelens/remote-web-streams
  • https://github.com/Seedess/postmessage-transferable-stream

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.

like image 65
bucabay Avatar answered Oct 19 '25 15:10

bucabay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!