4

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?

darkhorse
  • 8,192
  • 21
  • 72
  • 148
  • 1
    Should be able to. Is there something you have tried that is not working? – Mathias Feb 19 '19 at 23:49
  • @Mathias Thanks. And no not really, we don't have much experience with PWAs or WebRTC (coming from a server-client web background). So wanted to make sure before diving in. – darkhorse Feb 20 '19 at 12:49
  • I have no experience in doing so either, so best keep asking. I was just wondering if there was a problem to be fixed. – Mathias Feb 20 '19 at 13:29

3 Answers3

3

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.

The Mungler
  • 136
  • 1
  • 12
bucabay
  • 5,235
  • 2
  • 26
  • 37
2

You might technically be able to connect WebRTC in a service worker but they intentionally have short lifespans so the connection wouldn't last more than a couple of minutes.

abraham
  • 46,583
  • 10
  • 100
  • 152
0

yes works fine with Android, but getUserMedia won't work with PWA i mean in standalone mode

Sikki
  • 53
  • 1
  • 10