Skip to content

Conversation

Nkovaturient
Copy link

What was wrong?

  • This is an initial prototype of webrtc-feat for further guidance on py-webrtc-support implementation.

Issue #546

Implementations & Enhancements

  1. WebRTC Transport Setup
  • Established WebRTC Peer Connection (RTCPeerConnection) for direct communication.
  • Implemented SDP exchange via a WebSocket-based signaling server (listen_signaling).
  • Integrated WebRTC Data Channels (RTCDataChannel) for efficient peer-to-peer messaging.
  • Included robust error handling and logging improvements.
  1. Signaling Protocol & SDP Exchange
  • WebSockets are used for signaling to register and communicate SDP offers/answers.
  • Ensures smooth connection establishment between peers via offer-answer negotiation.
  • Added connection initiation for peer discovery (initiate_connection).
  1. Decentralized Peer Discovery (GossipSub & libp2p)
  • Integrated GossipSub to enable decentralized peer advertisement & discovery.
  • Implemented real-time peer discovery via Pubsub, where peers broadcast and subscribe to "webrtc-peer-discovery".
  • Enhanced peer connectivity through multiaddr parsing for WebRTC protocols

Questions

1.) Should we rely entirely on WebSockets for signaling, or use DHT as a fallback?

2)Multiaddr Handling & Validation

  • Does our current parse_webrtc_multiaddr() function correctly align with libp2p's expected formats?
  • Should we add additional multi-protocol support (/webrtc-direct, /wss, etc.)?

3.)Peer Discovery Optimization

  • Are we handling peer discovery in the most efficient manner using GossipSub?

To-Do

  • Implement STUN/TURN for NAT traversal.
  • Integrate AutoNAT for seamless peer connectivity.
  • Optimize connection stability for WebRTC behind firewalls.
  • Connection Management - Ensure robust connection handling in restricted network environments
  • Address network congestion and packet loss optimizations

Helpful resources

js-libp2p-webrtc
go-libp2p-webrtc

@pacrob
Copy link
Member

pacrob commented Apr 4, 2025

Just a note, py-libp2p is using trio for async functionality over asyncio.

@Nkovaturient
Copy link
Author

@pacrob noted, will modify, thanks!

@Nkovaturient
Copy link
Author

New Commit entails the following changes:-

🔧 webrtc.py – Core WebRTC Transport Layer for libp2p

We implemented a custom WebRTC transport for libp2p nodes using aiortc, designed to enable peer-to-peer communication over WebRTC data channels.

Key Components:

  • WebRTCRawConnection:

    • Wraps RTCDataChannel to provide a trio-based raw I/O interface.
    • Supports async read, write, and close.
  • WebRTCListener:

    • Listens for incoming WebRTC connection offers.
    • Accepts incoming data channels via a signaling protocol (/libp2p/webrtc/signal/1.0.0).
    • Handles the full SDP offer-answer exchange and prepares the raw connection.
  • WebRTCTransport:

    • Implements ITransport interface.
    • Handles peer connection setup:
      • Creating and opening data channels.
      • Exchanging SDP offers/answers.
      • Handling ICE candidates (optionally).
    • Adds support for GossipSub-based peer discovery over topic webrtc-peer-discovery.
    • Implements both dial() and create_listener() for WebRTC signaling.
  • parse_webrtc_multiaddr:

    • Validates and parses multiaddrs that include the /webrtc protocol.

🧪 test_loopback.py – Functional Loopback Testing

We created a loopback test using two libp2p BasicHosts to simulate a full peer-to-peer WebRTC connection over the new transport.

Highlights:

  • build_host(name):

    • Builds a libp2p host with:
      • TCP base transport.
      • Noise encryption.
      • Mplex stream multiplexer.
      • Registers a /ws WebSocket listen address
  • run_loopback_test():

    • Host B:

      • Initializes WebRTC transport.
      • Creates a listener and accepts incoming connections.
      • Reads a message and replies.
    • Host A:

      • Initializes another WebRTC transport.
      • Connects to Host B using dial().
      • Sends a message and reads the response.
  • Trio Nursery:

    • Orchestrates both client and server asynchronously in the same process.

Questions:

  1. the correct multiaddr for webrtc connections is: /ip4/127.0.0.1/tcp/9095/ws/p2p/{peer_id_b}/p2p-circuit/webrtc ?
  2. Is the webrtc.py setup connections, listener and dialing up to mark as per libp2p transport interface?
  3. our loopback-test is printing the two peerID of two hosts, further improving it to establish connections and exchange messages

@Nkovaturient
Copy link
Author

Commit: refactor-webrtc-setup-and-loopback-test

Description

- Key improvements to the` webrtc.py` file in the `libp2p.transport.webrtc` module:
  • Removed redundancies and unused imports
  • Ensured clean and consistent channel signaling and ICE candidate exchange
  • Added structured handling for stream-based SDP/ICE communication

✅ ICE Trickling: Implemented and now symmetric (caller/callee)
✅ Mplex: Fully integrated inside WebRTCRawConnection
✅ DataChannel: Integrated properly on both ends
✅ dial() and listen() lifecycle flows are connected

Updates:

  • WebRTCListener | Fully utilized for accepting connections from peers via set_stream_handler
  • WebRTCTransport | Used to establish offer/answer over the signaling stream
  • DataChannel Check | Ensures the channel is created before attempting communication
  • Connection Abstraction | Uses WebRTCRawConnection to simulate message exchange via write() and read()

Troubleshooting ahead:

  1. Fix test_loopback WebRTC transport errors and mypy typing mismatches
  2. mypy: Incompatible override of create_listener in webRTCTransport() of ITransport attribute
  3. Updating loopback test with correct handler and address formats
  4. Add valid multiaddrs to peerstore before dial attempts.

Test_loopback.py Result:

Screenshot (573)

Lint Run Result: All lint issues fixed except rst-file and mypy typing mismatches

Screenshot (572)

@seetadev
Copy link
Contributor

@Nkovaturient : Great progress. Please try and arrive at a good conclusion on the issues by Thursday.

@Nkovaturient
Copy link
Author

@Nkovaturient : Great progress. Please try and arrive at a good conclusion on the issues by Thursday.

@seetadev yeah, updating you shortly.

@Nkovaturient
Copy link
Author

@seetadev
Delighted to update you that:

  • webrtc transport, listener and connection are successfully implemented along with robust signalling service and generating certhash functionality for P2P connections via webrtc-direct
    — testing phase is underway (92%✅)✨🚀

@seetadev
Copy link
Contributor

@seetadev Delighted to update you that:

  • webrtc transport, listener and connection are successfully implemented along with robust signalling service and generating certhash functionality for P2P connections via webrtc-direct
    — testing phase is underway (92%✅)✨🚀

@Nkovaturient : Thank you so much for sharing the updates and code commits. Appreciate your contribution.

Wish if you could debug some of the build issues that are not passing after running the latest CI/CD checks.

Wish to also share that I discussed with rust-libp2p team on webrtc and documented a test plan for webrtc fallback relay testing matrix : #601

Please keep in touch with @Winter-Soren as he implements circuit relay and hole punching (support needed for webrtc in production).

@Nkovaturient
Copy link
Author

@seetadev Sure, actively working on the build issues + test suite
Also, I will coordinate with him regarding the webrtc fallback relay.
Thank you for informing.💯✨

@seetadev
Copy link
Contributor

@seetadev Sure, actively working on the build issues + test suite Also, I will coordinate with him regarding the webrtc fallback relay. Thank you for informing.💯✨

@Nkovaturient : We are almost through with the circuit relay PR. @Winter-Soren and I will be moving over to hole punching initiative in the coming week. Will keep you posted.

This will help you have the requisite NAT Traversal stack required in WebRTC. Autonat is complete and productionized. @Winter-Soren has ensured NAT Traversal features are production ready.

Please try AutoNAT functionality, which is fully ready: please visit autonat implementation video and testcases video
url at https://drive.google.com/drive/folders/1SxRBi5p23e45JiBMt5Lol7JUfADc0mdT?usp=sharing

@Nkovaturient
Copy link
Author

@seetadev humbly appreciate ur updates! Im taking care of it.

return actual_hash == expected_hash


def create_webrtc_direct_multiaddr(ip: str, port: int, peer_id: ID) -> Multiaddr:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are here 2 functions for direct_multiaddr.

if not isinstance(peer_id, ID):
peer_id = ID(peer_id)

base = f"/ip4/{ip}/udp/9000/webrtc-direct/certhash/{certhash}/p2p/{peer_id}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we get port also as optional argument with default value 9000 instead of hardcoding

@seetadev
Copy link
Contributor

@Nkovaturient : Hi Neha. Hope you're doing well.

Thanks again for your efforts. Wish to also thank @sukhman-sukh for his feedback and review to the PR.

To help accelerate finalization and testing, could you please add @sukhman-sukh as a collaborator to your current WebRTC PR? He’s been closely following the architecture and has already worked on related components, so his input and hands-on support should help us move things forward more quickly.

Thanks again for your continued efforts — this is an exciting milestone for the project, and we’re looking forward to seeing it land soon.

@Nkovaturient Nkovaturient mentioned this pull request Jul 20, 2025
7 tasks
@Nkovaturient
Copy link
Author

Hello there @seetadev
Adhering to this PR and due to various py dependencies issues and module imports err on windows after py-libp2p version updates, there was untypical, irrelevant issues arising on this branch while workin, hence created afresh branch (PR #780 ) and implemented the complete setup as efficiently as I could, with updates and dependencies checks.

Looking forward to collaborate wid @sukhman-sukh on further improvements and robust setup.

Thank you!

@seetadev
Copy link
Contributor

@Nkovaturient : Thanks for the update. Sure, please include the feedback points by @sukhman-sukh in the latest PR.

Wish to share that @sukhman-sukh is working on developing a NAT integrated transport layer production stack for webrtc (private to private) and integrating dcutr (hole punching), circuit relay and autonat in this issue: please visit #773 .

You can also ask him to include the NAT traversal integration, and contribute, integrate NAT traversal workflows in webrtc-direct efforts shared in the new PR at #780 . He did review this current PR (#557) before beginning the webrtc efforts and arrived at a set of key milestones he needed to achieve (in sync with the latest NAT traversal developments).

@seetadev seetadev closed this Jul 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants