Skip to content

Suggestion: circuit-relay-like module, but for negotiating WebRTC #361

@guysv

Description

@guysv

So, the best part of circuit-relay, is the fact that it allows to connect nodes where neither can listen for incoming connections.

Unfortunately, running a circuit-relay HOP node is expensive, and by default HOP is turned off.

tl;dr: I hacked js-libp2p-circuit into negotiating WebRTC signals using newly relayed connections, and establish a direct connection between unconnected peers using a helping 3rd party.

Such connection scheme is more considerate of "relay" nodes, because they only need to relay the signaling data, and not the entire connection.
Another plus, is that now browsers should be able to connect to one another directly.

I uploaded the modded js-libp2p-circuit repo and a demo to github:
You can view the diff to the original circuit-relay module in this PR: guysv/js-libp2p-webrtc-poc#1

To run the demo:

  1. npm install (note: I included a custom package-lock.json in the demo repo so that js-libp2p-switch will use my modded circuit module, if you feel extra paranoid, regenerate the package-lock.json and fill in guysv/js-libp2p-webrtc-poc#webrtc-poc
  2. run the hop node DEBUG=libp2p:circuit:* node src/hop.js
  3. run listener node DEBUG=libp2p:circuit:* node src/listener.js
  4. run dialer node DEBUG=libp2p:circuit:* node src/dialer.js
  5. look for those logs I added, which indicate that WebRTC connection was established with listener node
# dialer node
  libp2p:circuit:dialer Writing signal to circuit connection +21ms
  libp2p:circuit:dialer Writing signal to circuit connection +1ms
  libp2p:circuit:dialer Read signal from circuit connection +46ms
  libp2p:circuit:dialer Connected! +21ms
# listener node
  libp2p:circuit:stop Read signal from circuit connection +0ms
  libp2p:circuit:stop Read signal from circuit connection +3ms
  libp2p:circuit:stop Writing signal to circuit connection +11ms
  libp2p:circuit:stop Writing signal to circuit connection +2ms
  libp2p:circuit:stop Connected! +22ms

The connection is not really usable, but those logs indicate that a WebRTC channel was established, good enough for POC :)

Anyhow, That was a cool experiment. I wrote this issue to hear what you guys think about it. I tried to find material about WebRTC in Libp2p but the only modules I found were webrtc-star, which is a centralized solution, and webrtc-direct, which doesn't really offer interesting features that websockets lack. I think this could be a cool addition to the stack!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions