libp2p/js-libp2p-webrtc

Usage inside web workers

Closed this issue · 3 comments

For browser peer-to-peer applications, running a separate libp2p node in every tab might be wasteful. Shared workers are a common chioce here, where the worker handles the main p2p functions, while tabs connect to the worker via message channels.

Unfortunatelly WebRTC is not supported in workers, but there's a workaround: an open tab creates the RTCPeerConnection and tranfers RTCDataChannel to the worker.

I imagine there could be a webrtc transport implmentation where a shared worker coordintanes the connections, and open tabs register themselves with the worker to be able to open connections. Of course if the tab hosting the connection is closed, connection will be dropped. But as long as there's at least one tab open, a new connection could be opened.

I can try starting a PR with the proposed architecture, if libp2p team is interested in going into this direction.

tranfers RTCDataChannel to the worker.

I believe this only works in Safari and other browsers haven't implemented the rtcdatachannel as a transferable object. See the compatibility table here: https://developer.mozilla.org/en-US/docs/Web/API/RTCDataChannel#browser_compatibility

Closing this for now. If this changes, happy to reopen

Yeah, that's true. As a workaround while browsers are implementing that. You could transfer a WebStream instead and pipe it into the RTCDataChannel. There would be a performance penalty, but I imagine not a huge one.