Skip to content

initial-setup of py-webrtc #557

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 15 commits into
base: main
Choose a base branch
from

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.

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.

3 participants